Menu
CoddyTech

זמני המתנה בשרת

שיעור 7 מתוך 20 בקורס משחק 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

אתגר

בינוני

במטפל Ability, לפני הפעלת היכולת, יש לחזור כאשר:

  1. השם אינו מחרוזת המציינת יכולת,
  2. לשחקן אין שורש של דמות, או שהבריאות של ה-Humanoid שלו היא 0,
  3. GetServerTimeNow() מוקדם מזמן המוכנות של השחקן ליכולת הזו, המאפיין name .. "ReadyAt".

אחרת, יש להגדיר את המאפיין הזה לזמן הנוכחי ועוד Settings.Cooldowns[name], ואז להפעיל אותה. לחצו על Play, ואז על Check: הבדיקות מפעילות את ה-remote כפי שמנצל פרצות היה עושה.

נסו בעצמכם

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)
		end
	end
end

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

function abilities.Dash(player, character, root)
	root.AssemblyLinearVelocity = root.CFrame.LookVector * Settings.DashSpeed + Vector3.new(0, 15, 0)
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
end)

-- כל שחקן מקבל Kills בטבלת המובילים, וחרב בכל פעם שהוא נוצר מחדש
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats

	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)
quiz iconבחנו את עצמכם

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

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