Dash on Q
Lesson 7 of 25 in Coddy's Roblox Game: Battlegrounds course.
A dash is a push: setting a root's AssemblyLinearVelocity throws the character. But Roblox lets each player's device run the physics of their own character, so a push the server sets barely moves them. The server decides, and the player's own LocalScript pushes.
The server keeps its abilities in a table, by name, and a yes to a dash goes back to that player's client with FireClient, heard there by OnClientEvent:
remotes.Ability.OnClientEvent:Connect(function(name)
if name == "Hop" then
root.AssemblyLinearVelocity = Vector3.new(0, 50, 0)
end
end)Challenge
MediumMake Q dash: the server says yes, your device pushes.
- In
BattleServer, replace the print with anabilitiestable:abilities.Dash(player)callsremotes.Ability:FireClient(player, "Dash"). - The handler runs
abilities[name](player, character, root)when it exists. - In
BattleClient, onremotes.Ability.OnClientEventwith"Dash", set your root's velocity toroot.CFrame.LookVector * Settings.DashSpeed + Vector3.new(0, 15, 0).
Press Play and press Q: you dash forward. Then Check.
Try it yourself
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local lastSwing = {} -- player -> when they last swung
-- Roblox's own sword animations
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"
print("Swords deal " .. Settings.SwordDamage .. " damage")
-- A swing: the animation, then everyone in the box in front of you is hit
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()
-- the box in front of you, never your own body
local params = OverlapParams.new()
params.FilterDescendantsInstances = { character }
local box = root.CFrame * CFrame.new(0, 0, -3)
local hit = {} -- each humanoid once per swing
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
-- The client asks for an ability by name
remotes.Ability.OnServerEvent:Connect(function(player, name)
print(player.Name .. " asked for " .. name)
end)
-- Every player gets a sword every life
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)
-- The training dummy: its health bar follows its health, and a beaten
-- dummy comes back after a moment, a fresh copy in the same place
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)
All lessons in Roblox Game: Battlegrounds
Practice on your own: Roblox playground