Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

~~~~ 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) ~~~~~~

0
The variable "takeable" is already local when it enters the function. Therefore, your re-assigning lines should be "takeable = <boolean>". DeceptiveCaster 3761 — 5y
0
worked, thank you Nanzito123 -1 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

Ad

Answer this question