Creating a sword fighting system is one of the most exciting projects for beginner Roblox developers! Not only is it fun to build, but it teaches you fundamental scripting concepts like tool handling, detecting hits, and managing player health. In this tutorial, we’ll build a working sword that players can swing to deal damage to other players.
What You’ll Learn
By the end of this tutorial, you’ll understand how to:
- Create a sword tool that players can equip
- Detect when the sword hits another player
- Apply damage to players
- Add a cooldown system to prevent spam
Setting Up Your Sword
First, let’s create the basic sword object. In Roblox Studio:
- Insert a Tool object into ReplicatedStorage
- Name it “Sword”
- Inside the Tool, create a Part called “Handle” (this is required for tools to work)
- Design your Handle to look like a sword blade – make it long and narrow
- Insert a Script into the Tool (not a LocalScript)
The Handle is special because Roblox automatically attaches it to the player’s hand when they equip the tool. Any other parts you add will need to be welded to the Handle.
Understanding the Touch Event
The core of our sword system uses the Touched event. This fires whenever the sword’s Handle touches another part. We’ll use this to detect when we hit another player, but we need to be smart about it – the Touched event fires a lot, so we need a cooldown system.
Here’s why we need a cooldown: without one, a single swing could register dozens of touches in a fraction of a second, dealing way too much damage. A cooldown ensures each swing only deals damage once.
Writing the Sword Script
Let’s write the complete sword script. Add this code to the Script inside your Tool:
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local damage = 20
local cooldown = 1
local debounce = false
local function onTouched(hit)
-- Don't do anything if we're in cooldown
if debounce then return end
-- Check if we hit a player's character
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid and character ~= tool.Parent then
-- Make sure we don't hit ourselves!
debounce = true
-- Deal damage
humanoid:TakeDamage(damage)
-- Wait for cooldown, then allow damage again
task.wait(cooldown)
debounce = false
end
end
handle.Touched:Connect(onTouched)
Breaking Down the Code
Let’s understand what each part does:
Variables at the top: We define how much damage the sword deals (20), how long the cooldown is (1 second), and a debounce variable that tracks whether we’re currently in cooldown.
The debounce check: The first thing our function does is check if debounce is true. If it is, we return immediately without doing anything. This prevents multiple damage instances from a single swing.
Finding the Humanoid: When we touch something, we check if it’s part of a character by looking for a Humanoid. The Humanoid object controls player health and animations. We also check that character ~= tool.Parent to make sure players can’t damage themselves!
Dealing damage: We set debounce to true, use TakeDamage() to reduce the player’s health, then wait for our cooldown period before setting debounce back to false.
Adding a Swing Animation (Optional)
To make your sword feel better, you can add a swing animation. When the player activates the tool (clicks), play an animation:
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://YOUR_ANIMATION_ID"
tool.Activated:Connect(function()
local character = tool.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
local animator = humanoid:FindFirstChild("Animator")
local track = animator:LoadAnimation(animation)
track:Play()
end
end)
You’ll need to create or find a swing animation and replace YOUR_ANIMATION_ID with the actual ID.
Testing Your Sword
To test your sword:
- Copy the Sword tool from ReplicatedStorage to StarterPack
- Start a test server with at least 2 players (use the “Players” dropdown in the Test tab)
- Try swinging at the other player and watch their health decrease!
Recap and Next Steps
Congratulations! You’ve built a working sword fighting system. You learned how to use the Touched event, implement a debounce system to prevent spam, and detect hits on other players. These concepts apply to many other game mechanics, from projectiles to traps.
Ready to level up your sword? Try adding sound effects when the sword hits something, creating particle effects for slashes, or implementing a blocking mechanic. You could also explore creating different weapon types with varying damage and cooldown values!