Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I made a script and i was supposed to have a cooldown but it is not working. Why?

local Giver = script.Parent

local Gear = game.ReplicatedStorage.Morphs.SasukeClassic

local takeable = true

  

function onTouch(Brick)

if takeable then

local Player = Brick.Parent:findFirstChild("Humanoid")

if (Player ~= nil) then

local Location = game:GetService("Players"):GetPlayerFromCharacter(Player.Parent)

local New = Gear:Clone()

New.Parent = Location.Backpack

local takeable = false

wait(5)

local takeable = true

end

end

end

  

Giver.Touched:connect(onTouch)

2 comments

0

The variable "takeable" is already local when it enters the function. Therefore, your re-assigning lines should be "takeable = ".

0

worked, thank you

1 answer

Hey Nanzito!

You gotta make a variable that stops the script from running over and over. The wait() is only to wait before executing an another part of the script and it will not delay an another execution of the script.

I'll show you an example of how this can be solved:

local Giver = script.Parent
local Gear = game.ReplicatedStorage.Morphs.SasukeClassic
local takeable = true 

function onTouch(Brick)
    if takeable == true then
	    takeable = false --Added this to make this piece of code not-executable before we make it true again. 
	    local Player = Brick.Parent:findFirstChild("Humanoid")
	    if (Player ~= nil) then    
		    local Location = game:GetService("Players"):GetPlayerFromCharacter(Player.Parent)
		    local New = Gear:Clone()    
		    New.Parent = Location.Backpack        
		    
	    end
	    wait(5) -- Here's the cooldown
            takeable = true -- Making the script executable again.
    end
end

  

Giver.Touched:connect(onTouch)

I think you were kinda onto something with the "takeable" variable. You just placed it in some wrong spots. ^^

Happy coding!

Waterfox

This discussion is preserved as it was. New answers, comments, and votes are closed.