Menu
CoddyTech

הגע לבסיס

שיעור 4 מתוך 22 בקורס משחק Roblox: הגנת מגדלים של Coddy.

אויב שמגיע לסוף המסלול פוגע בבסיס. הבריאות של הבסיס שייכת לשרת, אבל המסך של כל שחקן יציג אותה, לכן שמור אותה בתור מאפיין של סביבת העבודה: מאפיין שמוגדר בשרת גלוי לכל לקוח.

workspace:SetAttribute("BaseHealth", Settings.BaseHealth)
print(workspace:GetAttribute("BaseHealth")) --> 20

damageBase(amount) מפחיתה את הבריאות, אך לעולם לא מתחת ל-0 (math.max), ומדפיסה אותה, כך שתוכל לעקוב אחרי הבסיס ב-Output עד שהיא תוצג ב-HUD. אחרי נקודת הציון האחרונה, האויב קורא לה ויוצא מהמשחק באמצעות Destroy, אחרת הגל לעולם לא יסתיים.

challenge icon

אתגר

קל

גרמו נזק לבסיס כשאויב מגיע אליו.

  1. אחרי מיון נקודות הציון, הגדירו את המאפיין של סביבת העבודה BaseHealth ל־Settings.BaseHealth.
  2. כתבו את damageBase(amount): הפחיתו את amount מ־BaseHealth, אך לא מתחת ל־0, והציגו את print("Base: " .. health).
  3. בסוף walkPath, קראו ל־damageBase(1) והשמידו את האויב.

לחצו על "שחק": ה־Zombie הולך אל הבסיס ונעלם, וב־Output מופיע Base: 19. לאחר מכן לחצו על "בדיקה".

נסו בעצמכם

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local enemies = workspace.Enemies

print("The base can take " .. Settings.BaseHealth .. " hits")

-- נקודות הציון, לפי הסדר (הן נקראות 1, 2, 3...)
local waypoints = workspace.Path.Waypoints:GetChildren()
table.sort(waypoints, function(a, b)
	return tonumber(a.Name) < tonumber(b.Name)
end)

-- כל שחקן מקבל Money בטבלת המובילים
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = Settings.StartMoney
	money.Parent = leaderstats
end)

-- שיעור 4: ההגעה לבסיס תופיע כאן

-- הולך עם אויב לאורך המסלול. MoveTo מפסיק לנסות אחרי 8 שניות ומודיע על כך
-- (MoveToFinished עם false), לכן שולחים אויב איטי שוב עד שהוא מגיע.
local function walkPath(enemy)
	local humanoid = enemy.Humanoid
	for i = 2, #waypoints do
		repeat
			humanoid:MoveTo(waypoints[i].Position)
		until humanoid.MoveToFinished:Wait() or not enemy.Parent
		if not enemy.Parent then
			return
		end
	end
end

-- אויב חדש בשער, שהולך לאורך המסלול
local function spawnEnemy(kind)
	local enemy = ServerStorage.Enemies[kind]:Clone()
	-- ממוקם ממש מעל נקודת הציון הראשונה
	enemy:PivotTo(waypoints[1].CFrame + Vector3.new(0, 5, 0))
	enemy.Parent = enemies
	task.spawn(walkPath, enemy)
	return enemy
end

-- אויב אחד, בינתיים
spawnEnemy("Zombie")

כל השיעורים ביחידה משחק Roblox: הגנת מגדלים

תרגלו בעצמכם: Roblox אונליין