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

Why is my for loop not using the wait function?

Asked by 2 years ago

I tried to making a thing where when you click a button it makes your clicks leaderstats go up by 1. I can click fast, so i wanted a cooldown on 1 or 0.5 seconds. Here is my script:

1for number = 1, 1 do
2    script.Parent.MouseButton1Click:Connect(function()
3        game.Players.LocalPlayer.leaderstats.Clicks.Value = game.Players.LocalPlayer.leaderstats.Clicks.Value + 1
4    end)
5 
6    wait(1)
7 
8end

Can you tell me why there is no cooldown? Thanks!

1 answer

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

Solution

I recommend using a debounce instead. A debounce is like a toggle of whether you should do this or not. It is crucial if you want to set up a cooldown.

Problem

The reason why your script isn't working is because you're only connecting MouseButton1Click every 1 second. Connecting the event makes it so that the function you connected to the event will call whenever the event is fired.

Final Script

Normal Script inside the button:

01local button = script.Parent
02local player = button:FindFirstAncestorOfClass("Player")
03repeat
04    player = button:FindFirstAncestorOfClass("Player")
05    task.wait()
06until player ~= nil
07 
08local debounce = true
09button.MouseButton1Click:Connect(function()
10    if debounce then -- if debounce is true
11        debounce = false
12 
13        local leaderstats = player:WaitForChild("leaderstats")
14        local Clicks = leaderstats:WaitForChild("Clicks")
15        Clicks.Value += 1
View all 21 lines...

Thanks @xInfinityBear for the suggestion to not use RemoteEvents!

1
Although this would work, I would not recommend using a Remote Event; especially when it is unnecessary. Exploiters could abuse this and give themselves as many clicks as they like. You only need to use a single server script; you can still get the player object from the server script, as the script would be a descendant of the player. xInfinityBear 1777 — 2y
0
Okay! T3_MasterGamer 2189 — 2y
Ad

Answer this question