Menu
CoddyTech

השהיות שרת

שיעור 8 מתוך 25 בקורס משחק Roblox: זירות קרב של Coddy.

מנצל לרעה שולט במכשיר שלו, ולכן הוא יכול להפעיל את Remotes.Ability מאה פעמים בשנייה, עם כל שם שהוא. זמן צינון ב־LocalScript יעצור רק שחקנים שפועלים בהגינות: השרת חייב לאכוף אותו.

השרת שומר, עבור כל שחקן וכל יכולת, מתי היא תהיה זמינה שוב, בתור מאפיין של השחקן (בשיעור הבא היא תוצג על המסך). workspace:GetServerTimeNow() הוא שעון שהשרת וכל הלקוחות מסכימים עליו:

local now = workspace:GetServerTimeNow()
if now < (player:GetAttribute(name .. "ReadyAt") or 0) then
	return
end
player:SetAttribute(name .. "ReadyAt", now + Settings.Cooldowns[name])

והשרת בודק מה נשלח אליו: מחרוזת שמציינת יכולת קיימת, משחקן שעדיין בחיים.

challenge icon

אתגר

בינוני

גרמו לשרת לדחות את מה שמנצל פרצות עלול לשלוח.

  1. במטפל Ability, החזירו אם typeof(name) ~= "string", אם היכולת לא קיימת או אם אין לשחקן דמות חיה.
  2. החזירו אם workspace:GetServerTimeNow() מוקדם מ-player:GetAttribute(name .. "ReadyAt") (או 0).
  3. אחרת, הגדירו את המאפיין הזה ל-now + Settings.Cooldowns[name], ואז הפעילו את היכולת.

לחצו על "שחק" ולחצו על Q פעמיים: המקף השני ממתין 3 שניות. לאחר מכן לחצו על "בדיקה".

נסו בעצמכם

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

local remotes = game.ReplicatedStorage.Remotes
local lastSwing = {} -- player -> מתי הניף את החרב בפעם האחרונה

-- אנימציות החרב של Roblox עצמה
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"

print("Swords deal " .. Settings.SwordDamage .. " damage")

-- הנפת חרב: האנימציה מתנגנת, ואז כל מי שנמצא בתיבה שלפניך נפגע
local function swing(player)
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or character.Humanoid.Health <= 0 then
		return
	end
	local now = time()
	if lastSwing[player] and now - lastSwing[player] < Settings.SwingCooldown then
		return
	end
	lastSwing[player] = now
	character.Humanoid.Animator:LoadAnimation(slash):Play()
	-- התיבה שלפניך, לעולם לא הגוף שלך
	local params = OverlapParams.new()
	params.FilterDescendantsInstances = { character }
	local box = root.CFrame * CFrame.new(0, 0, -3)
	local hit = {} -- כל humanoid פעם אחת בכל הנפת חרב
	for _, part in workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params) do
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid and not hit[humanoid] then
			hit[humanoid] = true
			humanoid:TakeDamage(Settings.SwordDamage)
			print(humanoid.Parent.Name .. " is at " .. humanoid.Health)
		end
	end
end

-- היכולות, לפי שם: מה כל אחת עושה
local abilities = {}

-- המכשיר של השחקן מזיז את הדמות שלו, ולכן ההסתערות מתבצעת שם
function abilities.Dash(player)
	remotes.Ability:FireClient(player, "Dash")
end

-- הלקוח מבקש יכולת לפי שם; השרת מבצע אותה
remotes.Ability.OnServerEvent:Connect(function(player, name)
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if abilities[name] and root then
		abilities[name](player, character, root)
	end
	-- שיעור 8: כאן יתווספו זמני ההמתנה של השרת
end)

-- כל שחקן מקבל חרב בכל פעם שהדמות שלו נולדת מחדש
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local sword = ServerStorage.Sword:Clone()
		sword.Parent = player.Backpack
		sword.Activated:Connect(function()
			swing(player)
		end)
	end)
end)

-- בובת האימון: מד הבריאות שלה משקף את מצב הבריאות שלה, ואם מביסים
-- את הבובה, היא חוזרת כעבור רגע כעותק חדש באותו מקום
local function setupDummy(dummy)
	local humanoid = dummy.Humanoid
	humanoid.HealthChanged:Connect(function(health)
		dummy.HealthBar.Back.Fill.Size = UDim2.fromScale(health / humanoid.MaxHealth, 1)
	end)
	humanoid.Died:Connect(function()
		local spot = dummy:GetPivot()
		task.wait(0.5)
		dummy:Destroy()
		task.wait(Settings.DummyRespawn)
		local fresh = ServerStorage.Dummy:Clone()
		fresh:PivotTo(spot)
		fresh.Parent = workspace
		setupDummy(fresh)
	end)
end
setupDummy(workspace.Dummy)

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

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