I assume the wait(30) is the cooldown part, and it's not working for a logical reason. Technically what you think would happen is; the code will function, you will wait for 30 seconds, in order ti re-use the weapon. But in reality, when the Activated event fires again, it ignores the 30 seconds, because it's gonna re-run a whole new part of code. The event fires a first time, it does what it's supposed to do, but it fires again even though 30 seconds haven't passed, because it creates a whole new part of code (or else called a thread) that doesn't have to wait for the first part of code to finish. Your idea is very correct! But what you need to add is a debounce instead of just a lonely wait(), and this concept is very simple: you make a variable saying whether the event has already fired or not (meaning it's set to false at first because it never ran), and when the event fires it becomes true, and it only goes back to false after a certain amount of time. And of course if a new part of code ran it wouldn't do it's thing while the variable is still true.
local tool = script.Parent
local debounce = false
tool.Activated:Connect(function()
if debounce == false then
debounce = true
local humanoid = tool.Parent:FindFirstChild("Humanoid")
humanoid.Health = humanoid.MaxHealth
humanoid.WalkSpeed = humanoid.WalkSpeed + 10
wait(30)
debounce = false
end
end)
2 comments
Can you perhaps use code blocks? To make this script easier to read? https://scriptinghelpers.org/questions/20098/how-to-codeblock
Add a Toggled function for it, like when you use it it becomes toggled for X seconds and then you cant use it while its toggled, and after this X seconds it becomes untoggled again and you can use it