Chiedi al Server
Lezione 6 di 25 del corso Gioco Roblox: Battlegrounds di Coddy.
Le abilità iniziano sul dispositivo del giocatore. BattleClient è uno LocalScriptContextActionService associa un'azione ai tasti e, con true come terzo argomento, aggiunge anche un pulsante sullo schermo di un telefono:
ContextActionService:BindAction("Wave", use, true, Enum.KeyCode.T)La funzione riceve il nome dell'azione e lo stato dell'input, Begin quando il tasto viene premuto. Le regole sono gestite dal server, quindi il client si limita a fare una richiesta tramite il RemoteEvent Remotes.Ability. Sul server, OnServerEvent riceve l'evento e Roblox passa per primo il giocatore che lo ha attivato:
remotes.Ability.OnServerEvent:Connect(function(player, name)
print(name .. " from " .. player.Name)
end)Sfida
MedioFai in modo che Q chieda al server uno scatto.
- In
BattleClient, scriviuse(name, state): suEnum.UserInputState.Begin, chiamaremotes.Ability:FireServer(name). - Associala:
ContextActionService:BindAction("Dash", use, true, Enum.KeyCode.Q). - In
BattleServer, collegaremotes.Ability.OnServerEventaprint(player.Name .. " asked for " .. name).
Premi «Gioca» e premi Q: nell'Output appare Player1 asked for Dash. Poi premi «Verifica».
Provalo tu
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local lastSwing = {} -- player -> quando ha sferrato l'ultimo fendente
-- Le animazioni della spada proprie di Roblox
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"
print("Swords deal " .. Settings.SwordDamage .. " damage")
-- Un fendente: parte l'animazione, poi vengono colpiti tutti quelli nel riquadro davanti a te
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()
-- il riquadro davanti a te, mai il tuo corpo
local params = OverlapParams.new()
params.FilterDescendantsInstances = { character }
local box = root.CFrame * CFrame.new(0, 0, -3)
local hit = {} -- ogni humanoid una sola volta per fendente
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
-- Ogni giocatore riceve una spada a ogni nuova vita
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)
-- Il manichino da allenamento: la sua barra della salute segue la sua salute e, se viene sconfitto, il
-- manichino ritorna dopo un po', come copia nuova nello stesso posto
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)
Tutte le lezioni di Gioco Roblox: Battlegrounds
2Client e Server
Chiedi al ServerEsercitati da solo: Playground Roblox