I have seen beginners having a hard time making a Sword Hitbox even though its really simple. Thats why I made this tutorial
(Disclaimer: We only sanity check the Hitbox and not the debounces)
First of all. Add this to your tool
Then put the code
Client:
-- // variables
local Player = game:GetService("Players").LocalPlayer
local Character
repeat
Character = Player.Character or Player.CharacterAdded:Wait()
task.wait()
until Character
local Humanoid = Character:WaitForChild("Humanoid")
local Handle = script.Parent:WaitForChild("Handle")
local Hit = script.Parent:WaitForChild("Hit")
local Animation = Humanoid:LoadAnimation(script.Parent:WaitForChild("Animation"))
local Touched = {}
local Swing = false
local SwingDebounce = false
local SwingTime = .3
local SwingDebounceTime = .4
-- // Connect
script.Parent.Activated:Connect(function()
if SwingDebounce then return end
SwingDebounce = true
Swing = true
Animation:Play()
task.wait(SwingTime)
Swing = false
Touched = {}
task.wait(SwingDebounceTime)
SwingDebounce = false
end)
Handle.Touched:Connect(function(hit)
if not (hit.Parent:FindFirstChild("Humanoid") or Swing) or Touched[hit.Parent] then
return
end
-- do hit fx or smth
Touched[hit.Parent] = true
Hit:FireServer(hit.Parent)
end)
Server:
-- // Variables
local Hit = script.Parent:WaitForChild("Hit")
local MagRange = 5
local Damage = 10
-- // Connect
Hit.OnServerEvent:Connect(function(player, hitChar)
local magnitude = (player.Character:FindFirstChild("HumanoidRootPart").Position - hitChar:FindFirstChild("HumanoidRootPart").Position).Magnitude
if magnitude <= MagRange then
-- confirmed hit, fire all clients fx
hitChar:FindFirstChild("Humanoid"):TakeDamage(Damage)
end
end)
You may have noticed that I used Touched on the Client and Magnitude on the Server
Why? Because its more smoother and fluid hitbox. Touched on the client has better responsiveness while on the Magnitude on the server we just have to check if its actually close on the enemy and deal the damage
Thats basically it. I hope you learned something on this tutorial, its actually my first tutorial on the Dev Forum, if I made any mistakes please let me know. Thank you