Hammer not working as intended

I am trying to make a hammer that does 40 damage upon impact of another player. However I’ve ran into one issue.

local on = false
local reload = false

script.Parent.Activated:Connect(function()
if reload == false then
game.ReplicatedStorage.RockAnimation:FireServer(game.Workspace.TestAnim)
reload = true
on = true
wait(1.7)
reload = false
end
end)

script.Parent:WaitForChild(“Handle”).Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) and on == true then
local hum = hit.Parent.Humanoid
hum.Health -= 33
else return end
on = false
end)

When I run the script, the hammer damages the player. However, if I just click, with the hammer not even touching the player, and then touch the player with the hammer, the player still takes damage. What is the problem here, and how could I fix it?

I’ve looked at a lot of tutorials on youtube, but they end up having the same problem as I do.

That’s because the variable on is set back to false within the code body for the Touched event. To have it work as intended, write it inside your Activated code body as well.

script.Parent.Activated:Connect(function()
    if reload == false then
        game.ReplicatedStorage.RockAnimation:FireServer(game.Workspace.TestAnim)
        reload = true
        on = true
        wait(1.7)
        reload = false
        on = false -- <<< like this
    end
end)

It works now! Thank you so much!

1 Like