服务器冷却时间
Coddy Roblox 游戏:竞技场对战 课程的第 8 课,共 25 课。
利用者可以控制自己的设备,因此可以每秒触发一百次 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])服务器还会检查收到的内容:它必须是一个字符串,命名一种真实存在的能力,并且发送者必须是存活的玩家。
挑战
中等让服务器拒绝利用者可能发送的内容。
- 在
Ability处理程序中,如果typeof(name) ~= "string"、该能力不存在,或玩家没有存活的角色,就返回。 - 如果
workspace:GetServerTimeNow()早于player:GetAttribute(name .. "ReadyAt")(或 0),就返回。 - 否则,将该属性设为当前时间 +
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 = {} -- 每次挥剑只击中每个人形角色一次
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 在线编辑器