Swing
Lesson 2 of 25 in Coddy's Roblox Game: Battlegrounds course.
With a tool in hand, a click (or a tap) fires the Tool's Activated event, on the server too. A swing plays Roblox's own sword slash: an Animation names it by its id, and the character's Animator, inside its Humanoid, loads it into a track to play:
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"
character.Humanoid.Animator:LoadAnimation(slash):Play()Without a limit, a player could click as fast as they like, so a swing remembers when the last one was, per player, in a table, and refuses another within Settings.SwingCooldown seconds.
Challenge
EasySwing the sword on a click.
- Near the top:
local lastSwing = {}and theslashAnimation, idrbxassetid://522635514. - Write
swing(player): return if the character is dead or swung less thanSettings.SwingCooldownago. - Else set
lastSwing[player] = nowand play the slash oncharacter.Humanoid.Animator. - Connect the sword's
Activatedtoswing(player).
Press Play, hold the sword and click: your character slashes. Then Check.
Try it yourself
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
print("Swords deal " .. Settings.SwordDamage .. " damage")
-- Lesson 2: Swing goes here
-- 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
end)
end)
All lessons in Roblox Game: Battlegrounds
Practice on your own: Roblox playground