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

How Do I Add An Cooldown On This Script?? [closed]

Asked by 4 years ago

Hello, I'm A Beginner In Scripting And I'm Making A Simulator Game And I'm Making A Script And I Don't Know How To Add A Cooldown In Here So Could Anyone Do It?

local Players = game:GetService("Players")
local sword = game.ReplicatedFirst.ClassicSword

local part = script.Parent
local function onTouched(part)  
    local player = Players:GetPlayerFromCharacter(part.Parent)
    if not player then return end
    local backpack = player.Backpack
    sword:Clone().Parent = backpack
end
part.Touched:Connect(onTouched)

Closed as Not Constructive by DesertusX, AntiWorldliness, imKirda, JesseSong, User#29913, TaxesArentAwesome, matiss112233, and killerbrenden

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

If you want to add a cooldown to how many times a player can touch, then here is what I would do. Not sure if it will work, but it is good to give it a try!

local Players = game:GetService("Players")
local sword = game.ReplicatedFirst.ClassicSword
local cooldown = false -- Sets the variable cooldown to false


local part = script.Parent
local function onTouched(part) 
 if cooldown == false then-- If cooldown is false then
    cooldown = true -- cooldown will turn into true
        local player = Players:GetPlayerFromCharacter(part.Parent)
        if not player then return end
            local backpack = player.Backpack
            sword:Clone().Parent = backpack
        wait(10) -- However long you want the cooldown time to be
    cooldown = false [[-- After all the above has runned, cooldown will return to false so it can be touched again!--]]
end

part.Touched:Connect(onTouched)
0
It did'nt Work Derrick_Dage -128 — 4y
0
what was the output? Lightning_Game27 232 — 4y
0
The out put Is " Workspace.Part.Sword Giver:8: Expected 'then' when parsing if statement, got 'cooldown' " Derrick_Dage -128 — 4y
0
Lightning just forgot to add "then" to his if statement. AntiWorldliness 868 — 4y
View all comments (7 more)
0
yeeaahhh.. i will fix that Lightning_Game27 232 — 4y
0
now it should work Lightning_Game27 232 — 4y
0
now it should work Lightning_Game27 232 — 4y
0
now it should work Lightning_Game27 232 — 4y
0
now it should work Lightning_Game27 232 — 4y
0
now it should work Lightning_Game27 232 — 4y
0
now it should work Lightning_Game27 232 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

To make a cooldown with scripting, we use something called debounce. First, you would assign a value that a player sticks with until they trigger a function and their value is temporarily changed until the function is completed. In order for the function to run, however, the player is required to have a specific value which is their original value. Here's a demonstration:

-- Let's say the script's parent is a TextButton

local debounce = false -- the player starts out with false

script.Parent.MouseButton1Click:Connect(function()
    if debounce == false then -- the player's debounce needs to be false; otherwise, the rest of the code in this if statement will not run
        debounce = true -- if the player clicks the button while their debounce is set to true, the code will not run because the if statement is checking if their debounce is false which it is not anymore
        script.Parent.Text = "Click accepted"

    else
        print("Cooldown is not over yet!")
    end

    wait(2) -- player has to wait 2 seconds until their debounce is set back to false again
    debounce = false -- now, they are able to repeat the function
end)
0
but debounce is just a variable that is set to false. this means even the word "hello" can be set as the variable instead of debounce, and it will still work. Lightning_Game27 232 — 4y
0
That's right. It doesn't matter what you call it. AntiWorldliness 868 — 4y