Menu
CoddyTech

向服务器发出请求

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

技能从玩家的设备端开始。BattleClient 是 HUD 中的一个 LocalScript。ContextActionService 会将一个操作绑定到按键;将 true 设为第三个参数后,它还会在手机屏幕上为这个操作添加一个按钮:

ContextActionService:BindAction("Wave", use, true, Enum.KeyCode.T)

这个函数会获取操作的名称和输入状态。当按键按下时,状态为 Begin。规则由服务器负责,因此客户端只会通过 RemoteEvent Remotes.Ability 发出请求。在服务器上,OnServerEvent 会接收这个事件,Roblox 会将触发事件的玩家作为第一个参数传入:

remotes.Ability.OnServerEvent:Connect(function(player, name)
	print(name .. " from " .. player.Name)
end)
challenge icon

挑战

中等

让 Q 向服务器请求冲刺。

  1. 在 BattleClient 中,编写 use(name, state):当状态为 Enum.UserInputState.Begin 时,调用 remotes.Ability:FireServer(name)。
  2. 绑定它:ContextActionService:BindAction("Dash", use, true, Enum.KeyCode.Q)。
  3. 在 BattleServer 中,将 remotes.Ability.OnServerEvent 连接到 print(player.Name .. " asked for " .. name)。

按“开始”,然后按 Q:输出会显示 Player1 asked for Dash。然后按“检查”。

亲手试试

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

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

-- 每个玩家每次重生都会获得一把剑
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 在线编辑器