Menu
CoddyTech

Scatto con Q

Lezione 7 di 25 del corso Gioco Roblox: Battlegrounds di Coddy.

Uno scatto è una spinta: impostare AssemblyLinearVelocity della radice lancia il personaggio. Ma Roblox permette al dispositivo di ogni giocatore di eseguire la fisica del proprio personaggio, quindi una spinta impostata dal server lo muove appena. Decide il server, mentre il LocalScript del giocatore applica la spinta.

Il server conserva le sue abilità in una tabella, per nome, e il via libera a uno scatto torna al client di quel giocatore con FireClient, dove viene ricevuto da OnClientEvent:

remotes.Ability.OnClientEvent:Connect(function(name)
	if name == "Hop" then
		root.AssemblyLinearVelocity = Vector3.new(0, 50, 0)
	end
end)
challenge icon

Sfida

Medio

Fai in modo che Q attivi lo scatto: il server dà l’ok, il tuo dispositivo ti spinge.

  1. In BattleServer, sostituisci la stampa con una tabella abilities: abilities.Dash(player) chiama remotes.Ability:FireClient(player, "Dash").
  2. Il gestore esegue abilities[name](player, character, root) quando esiste.
  3. In BattleClient, all’evento remotes.Ability.OnClientEvent con "Dash", imposta la velocità della tua radice su root.CFrame.LookVector * Settings.DashSpeed + Vector3.new(0, 15, 0).

Premi «Gioca» e premi Q: scatti in avanti. Poi premi «Verifica».

Provalo tu

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

local remotes = game.ReplicatedStorage.Remotes
local lastSwing = {} -- player -> quando ha sferrato l'ultimo colpo

-- Le animazioni della spada 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

-- Il client richiede un'abilità per nome
remotes.Ability.OnServerEvent:Connect(function(player, name)
	print(player.Name .. " asked for " .. name)
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 ricompare dopo un momento, come nuova copia 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

Esercitati da solo: Playground Roblox