Menu
CoddyTech

冷却时间显示

Coddy Roblox 游戏:竞技场对战 课程的第 9 课,共 25 课。

玩家应该能看到技能何时就绪。HUD 中有一个 Cooldowns 框架,其中每个技能都有一个标签;服务器已经在玩家身上保存了 DashReadyAt,客户端可以读取它。

倒计时每帧都会变化,因此客户端会在 RunService.Heartbeat 上重绘倒计时,该事件每帧触发一次。string.format 会保留一位小数:

string.format("%s %.1f", "Dash", 2.345) --> "Dash 2.3"

客户端只显示冷却时间。是否允许使用仍由上一课中的服务器检查决定。

challenge icon

挑战

中等

在 HUD 上显示每个技能的冷却时间。

  1. 在 BattleClient 中:添加 RunService 和 local hud = script.Parent。
  2. 每次触发 RunService.Heartbeat 时,遍历 local shown = { "Dash" } 中的每个名称:left = (me:GetAttribute(name .. "ReadyAt") or 0) - workspace:GetServerTimeNow()。
  3. 当 left > 0 时,将 hud.Cooldowns[name].Text 设为 string.format("%s %.1f", name, left);否则设为 name .. " ready"。

按下 “开始”,再按 Q:标签会倒计时,直到显示 Dash ready。然后点击 “检查”。

亲手试试

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)
	if typeof(name) ~= "string" or not abilities[name] then
		return
	end
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or character.Humanoid.Health <= 0 then
		return
	end
	local now = workspace:GetServerTimeNow()
	if now < (player:GetAttribute(name .. "ReadyAt") or 0) then
		return
	end
	player:SetAttribute(name .. "ReadyAt", now + Settings.Cooldowns[name])
	abilities[name](player, character, root)
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)
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

Roblox 游戏:竞技场对战中的所有课

自己练习: Roblox 在线编辑器