I am trying to make a script where pressing F would headbutt an enemy and damages them. The problem, however, is that after pressing F once, you no longer need to press F again to damage them, you just need to walk close to them.
local CanDoDamage = false
local UserInputService = game:GetService("UserInputService")
local Damage = 15
local Player = game.Players.LocalPlayer
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
local Animation = script.Parent.Humanoid:LoadAnimation(script.Animation)
Animation:Play()
script.Parent.Head.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and CanDoDamage == false then
CanDoDamage = true
hit.Parent:FindFirstChild("Humanoid"):TakeDamage(Damage)
print("You attacked!")
wait(1)
CanDoDamage = false
end
end)
end
end)
I tried adding the CanDoDamage variable to prevent this, but it still doesn't seem to work. How can I fix this? Also, this script isn't meant to work in multiplayer as there is no remote event or function.

1 comment
just use wait()