I'm making a sword for an rpg and I want the sword to do damage every time you swing but only be able to swing once every second. While trying to accomplish this, I've also ran into the problem of the sword doing damage before you swing and if it's just simply touching the mob, which is not what I want.
Thanks in advance ;)
Use a debounce variable something like isSwinging. To solve your first problem, first check to see if isSwinging is false, if it is then set it to true and swing the sword then wait 1 second and set isSwining to false. To solve your second problem, check to see if ifSwinging is true in the script that deals damage that way it won't deal damage when you don't swing your sword!
So I have a script and a local script inside the sword with an attack animation inside the local script.
Script:
local debounce = true - script.Parent.Touched:Connect(function(p) --event local humanoid = p.Parent:FindFirstChild("Humanoid") if debounce == true and humanoid then debounce = false humanoid:TakeDamage(math.random(1,2)) end end)
Local script:
local player = game:GetService("Players").LocalPlayer local char = player.Character or player.CharacterAdded:Wait() local canplay = true script.Parent.Activated:Connect(function() local attack = char:WaitForChild("Humanoid"):LoadAnimation(script.Attack) if canplay == true then attack:Play() canplay = false wait(1) attack:Stop() canplay = true end end)
So the animation plays only every 1 second but the sword still won't do any damage