Menu
CoddyTech

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 icon

Challenge

Easy

Swing the sword on a click.

  1. Near the top: local lastSwing = {} and the slash Animation, id rbxassetid://522635514.
  2. Write swing(player): return if the character is dead or swung less than Settings.SwingCooldown ago.
  3. Else set lastSwing[player] = now and play the slash on character.Humanoid.Animator.
  4. Connect the sword's Activated to swing(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