Menu
CoddyTech

Colpo a terra

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

Una seconda abilità, con E: un colpo a terra che colpisce tutti quelli intorno a te. workspace:GetPartBoundsInRadius(position, radius, params) trova le parti entro una certa distanza da un punto, come la hitbox del fendente, ma circolare:

for _, part in workspace:GetPartBoundsInRadius(root.Position, 6, params) do

Ora il gioco ha due modi per ferire qualcuno, e ne arriveranno altri. Ogni colpo passa attraverso un'unica funzione, hurt(humanoid, amount), così una regola sui danni (il blocco, a breve) viene scritta una sola volta. Inoltre, lascia in pace un Humanoid già sconfitto.

challenge icon

Sfida

Medio

Aggiungi uno schianto a terra con E.

  1. Sopra swing, scrivi hurt(humanoid, amount): TakeDamage(amount) a meno che la sua Health non sia 0. Swing la chiama invece di TakeDamage e della stampa.
  2. Aggiungi abilities.Slam(player, character, root): ogni Humanoid (una sola volta, non il tuo) in GetPartBoundsInRadius(root.Position, Settings.SlamRadius, params) riceve hurt(humanoid, Settings.SlamDamage).
  3. In BattleClient, associa "Slam" a E e aggiungilo a shown.

Premi «Gioca» e premi E vicino al manichino: la sua barra scende. 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 nell'area 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()
	-- l'area davanti a te, mai il tuo stesso 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

-- Le abilità, per nome: cosa fa ciascuna
local abilities = {}

-- il dispositivo del giocatore muove il suo personaggio, quindi lo scatto avviene lì
function abilities.Dash(player)
	remotes.Ability:FireClient(player, "Dash")
end

-- Lezione 10: Ground Slam va qui

-- Il client richiede un'abilità per nome. Il server verifica che sia una vera
-- abilità, che il giocatore sia vivo e che il suo tempo di ricarica sia terminato. Il momento in cui sarà di nuovo pronta
-- è un attributo del giocatore, così può essere mostrato sul suo schermo.
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)

-- 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, quando 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