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

How do i add a cooldown script that informs player when he can click again?

Asked by 6 years ago

Hi. I have managed to msg player when player clicks to get item if player already have that item in inventory but i have no idea how to add a cooldown on it instead of checking backpack.

Hope someone can help me change from this to cooldown timer for player (I would wish to have option to do it for all players and option for only player that have clicked. If not possible then only for player who clicked)

NOTE: I'm a compleete noob in scripting

function onClicked() do
                    --for i=1, 1 do
                    --while wait(5) do

        for _, player in ipairs(game.Players:GetPlayers()) do

            if player:FindFirstChild("Backpack") then
                if player:FindFirstChild("Backpack"):FindFirstChild("Blue Master Key")
                or
                player:FindFirstChild("Backpack"):FindFirstChild("Green Master Key")
                or
                player:FindFirstChild("Backpack"):FindFirstChild("Red Master Key")
                or              
                player:FindFirstChild("Backpack"):FindFirstChild("Yellow Master Key")
                then
                local M = Instance.new("Hint")
                M.Parent = game.Workspace -- Don't edit or change this.
                M.Text = "You allredy own a Master Key"
                wait(3) -- Wait to change insert thing.
                M.Text = "You can only have 1 Master Key in your inventory"
                wait(3) -- Waits to change then Remove insert differ thing.
                M:Remove()
                        else do
------HERE GOES VERRY HUGE SCRIPT
end
end
end
end
end
end
end
end
end
end
--------------------------------------------------------------------
script.Parent.ClickDetector.MouseClick:Connect(onClicked)

2 answers

Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

hello, kexiGamer!

This just adds a debounce to your code!

local deb = false
function onClicked() do
    if not deb then
    deb = true

    for _, player in ipairs(game.Players:GetPlayers()) do

            if player:FindFirstChild("Backpack") then
                if player:FindFirstChild("Backpack"):FindFirstChild("Blue Master Key")
                or
                player:FindFirstChild("Backpack"):FindFirstChild("Green Master Key")
                or
                player:FindFirstChild("Backpack"):FindFirstChild("Red Master Key")
                or              
                player:FindFirstChild("Backpack"):FindFirstChild("Yellow Master Key")
                then
                local M = Instance.new("Hint")
                M.Parent = game.Workspace -- Don't edit or change this.
                M.Text = "You allredy own a Master Key"
                wait(3) -- Wait to change insert thing.
                M.Text = "You can only have 1 Master Key in your inventory"
                wait(3) -- Waits to change then Remove insert differ thing.
                M:Remove()
                        else do
------HERE GOES VERRY HUGE SCRIPT
end
end
end
end
end
end
end
end
end
end
wait(120) -- Cooldown
deb = false
end
--------------------------------------------------------------------
script.Parent.ClickDetector.MouseClick:Connect(onClicked)

This Cooldown is called debounce

This script shows countdown:

local deb = false
local CountdownTime = 120 -- You can change the countdown time =D

-- Don't change above!
local Countdown = CountdownTime
local Text = script.Parent.Text
function onClicked() do
    if not deb then
    deb = true

        for _, player in ipairs(game.Players:GetPlayers()) do

            if player:FindFirstChild("Backpack") then
                if player:FindFirstChild("Backpack"):FindFirstChild("Blue Master Key")
                or
                player:FindFirstChild("Backpack"):FindFirstChild("Green Master Key")
                or
                player:FindFirstChild("Backpack"):FindFirstChild("Red Master Key")
                or              
                player:FindFirstChild("Backpack"):FindFirstChild("Yellow Master Key")
                then
                local M = Instance.new("Hint")
                M.Parent = game.Workspace -- Don't edit or change this.
                M.Text = "You allredy own a Master Key"
                wait(3) -- Wait to change insert thing.
                M.Text = "You can only have 1 Master Key in your inventory"
                wait(3) -- Waits to change then Remove insert differ thing.
                M:Remove()
        else do
------HERE GOES VERRY HUGE SCRIPT
end
end
end
end
end
end
end
end
end
else
    script.Parent.Text = "Can't click now, must wait: " .. Countdown .. " seconds!"
    wait(5)
    script.Parent.Text = Text
end
while Countdown > 0 do
    Countdown = Countdown - 1
end
local Countdown = CountdownTime
deb = false
end
--------------------------------------------------------------------
script.Parent.ClickDetector.MouseClick:Connect(onClicked)

Hope it helps!

Good Luck with your games!

0
Cheers m8! This helps a little bit so they can not spam the button. But how do i add a msg that tells them how long they have to wait untill they can click again? kexiGamer 21 — 6y
0
You need to edit the script a little to do this, but you have to create a new variable and use a loop to make it lower the value Leamir 3138 — 6y
0
i just want to block player from clicking again and again and getting an item. But i want the player to get msg about when he/she can get the item again- So countdown of the cooldown. I realy hope that u can help me m8 kexiGamer 21 — 6y
0
I`ll edit the script and make it show the countdown Leamir 3138 — 6y
Ad
Log in to vote
0
Answered by
xdeno 187
6 years ago
Edited 6 years ago

I believe what you're looking for is debounce: https://wiki.roblox.com/index.php?title=Debounce

I added it into your script, hopefully this is what you want and it works out for you.

I also indented your code to make it look neater and see that you have unnecessary ends that don't add up at the end.

local buttonPressed = false
function onClicked() do
        --for i=1, 1 do
        --while wait(5) do
        if not buttonPressed then
            buttonPressed = true
            for _, player in ipairs(game.Players:GetPlayers()) do
                if player:FindFirstChild("Backpack") then
                    if player:FindFirstChild("Backpack"):FindFirstChild("Blue Master Key")
                        or
                        player:FindFirstChild("Backpack"):FindFirstChild("Green Master Key")
                        or
                        player:FindFirstChild("Backpack"):FindFirstChild("Red Master Key")
                        or              
                        player:FindFirstChild("Backpack"):FindFirstChild("Yellow Master Key")
                        then
                        local M = Instance.new("Hint")
                        M.Parent = game.Workspace -- Don't edit or change this.
                        M.Text = "You allredy own a Master Key"
                        wait(3) -- Wait to change insert thing.
                        M.Text = "You can only have 1 Master Key in your inventory"
                        wait(3) -- Waits to change then Remove insert differ thing.
                        M:Remove()
                        buttonPressed = false
                    else do
                            ------HERE GOES VERRY HUGE SCRIPT
                        end
                    end
                end
            end
        end
    end
end
--------------------------------------------------------------------
script.Parent.ClickDetector.MouseClick:Connect(onClicked)
0
it doesn't inform player when he/she can click again. =( And there is no timer on this one kexiGamer 21 — 6y

Answer this question