Also, it’s because every time your event is fired, a new thread is created (think of it as a new script to run whatever that is contained inside the event). This is to prevent any unexpected errors from pausing the entire execution of the script.
To add a cooldown or a debounce, refer to this tutorial.
local debounce = false
local model = script.dmg
script.Parent.Activated:Connect(function()
if debounce then
return
end
debounce = true
local clone = model:Clone()
script.Sound:Play()
clone.Parent = script
task.wait(.9)
clone:Destroy()
debounce = false
end)
There are multiple ways to do this, but this is my preferred:
local cooldown = 1
local lastActivate = 0
script.Parent.Activated:Connect(function()
if os.clock - lastActivate < cooldown then
return
end
lastActivate = os.clock()
local clone = model:Clone()
script.Sound:Play()
clone.Parent = script
task.wait(.9) --wait() is deprecated, use task.wait() instead.
clone:Destroy()
end)