I'm trying to make a script where if the player hits an object, it will change the value of something to true, then wait 5 seconds before it changes it back to false. I want it to wait another 5 seconds if it has been hit before it gets a chance to change the value back to false. However, I'm having trouble with this. My current script continues to change the value to false, even if I hit it again. (It's supposed to wait 5 seconds)
local Animal = script.Parent local Attacker = Animal.Attacker local TakingDamage = Animal.IsTakingDamage script.Parent.Touched:Connect(function(tuc) if tuc.name == "Blade" then Attacker.Value = tuc.Parent.Parent.Name TakingDamage.Value = true print("TakingDamage") wait(5) Attacker.Value = "nil" TakingDamage.Value = false end end)
How can I stop it from setting the value false if it has been hit again? Feel free to ask questions. Help is appreciated.
At first i thought it was just a simple debouncing that was required but after re-reading your question this was a more interesting problem.
I added a while loop and a waitTime, basically the waitTime will reset back to 5 seconds if the thing was touched before waitTime counts down to 0 or if the waitTime already reaches 0
If the waitTime reaches 0 it will toggle the Touched value to false, allowing the thing to be touched again
My solution:
local Animal = script.Parent local Attacker = Animal.Attacker local TakingDamage = Animal.IsTakingDamage local Touched = false local breaker = false local OriginalWaitTime = 5--change this value to however long you want it to wait in seconds local waitTime = 5 script.Parent.Touched:Connect(function(tuc) breaker = true if tuc.name == "Blade" and not Touched then Touched = true breaker = false Attacker.Value = tuc.Parent.Parent.Name TakingDamage.Value = true print("TakingDamage") end while waitTime>=0 do waitTime = waitTime - 1 if breaker then--if the thing has been touched inappropriately this will trigger and end the loop and a new while loop is created and resets the waititme back to 5. waitTime = OriginalWaitTime breaker = false break end if waitTime <0 then--after 5 seconds Touched becomes false and player can touch again Touched = false breaker = false waitTime = OriginalWaitTime Attacker.Value = "nil" TakingDamage.Value = false break end wait(1) end end)
what you want is a debounce, which is pretty much a bool variable like debounce = false
and you name debounce anything like died = true
, dying = false
like this example:
local debounce = false script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") and debounce then -- the if statement only executes the code if "debounce" is false debounce = true -- makes it so we cant execute the code and sets it true print("hello") wait(5) -- waits 5 sec til we can execute the code debounce = false -- sets it to false end end)
like this
local Animal = script.Parent local Attacker = Animal.Attacker local TakingDamage = Animal.IsTakingDamage local canTouch = false script.Parent.Touched:Connect(function(tuc) if tuc.name == "Blade" and canTouch then canTouch = true Attacker.Value = tuc.Parent.Parent.Name TakingDamage.Value = true print("TakingDamage") wait(5) Attacker.Value = "nil" TakingDamage.Value = false canTouch = false end end)
also there can be other forms of a Debounce
like using numbers such as like this
debounce = 0 script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") and debounce == 0 then debounce = 1 print("hello") wait(5) debounce = 0 end end)
You can use a quite simple method;
create a simple Script.
then add an IntValue named Cooldown inside the script.
add the lines below to the Script!
-- The Power Of "Events"! local Animal = script.Parent local Attacker = Animal.Attacker local TakingDamage = Animal.IsTakingDamage local cool = script.Cooldown loop = false script.Parent.Touched:Connect(function(hit) if hit.Name == "Blade" then Attacker.Value = hit.Parent.Parent.Name TakingDamage.Value = true cool.Value = cool.Value + 5 loop = true end end) cool.Changed:Connect(function() if cool.Value == 0 then TakingDamage.Value = false Attacker.Value = nil loop = false wait(1) end end) while loop == true do wait(1) cool.Value = cool.Value - 1 end -- Feel free to edit it!
-- Hope i helped.
---- AIphanium.