I want to give 5 coins every time someone touches a block. But I don't want them to rapidly touch it and get a lot of money. so I want to put a 120 seconds time before they can get their money again. But I don't know how to do this.
how do I make a script work for 120 seconds per person?
2 answers
This can be achieved by tick(). This allows us to set a timestamp that we can compare time passed to. It would look like this:
local timeStamp = tick() --// Get's current time at the moment called.
local totalTime = 5 --// Time expected to pass
repeat wait() until (tick() - timeStamp) >= totalTime --// Compare time now to time passed.
print(totalTime.." Passed!")
Though this method above seems fancy, there is a totally simpler way, if you'd like to use this route. Just use wait(s)
print("Began")
wait(120)
print("Finished")
Hope this helps! If so, don't forget to accept this answer!
Try something like this:
local timeouts = {}
local function findHumanoid(part)
if not part or not part:IsDescendantOf(workspace) then return end
local current = part.Parent
while current ~= workspace then
local humanoid = current:FindFirstChildWhichIsA('Humanoid')
if humanoid than return humanoid end
current = current.Parent
end
end
script.Parent.Touched:Connect(function(part)
local humanoid = findHumanoid(part)
if not humanoid then return end
local player = game:GetService('Players'):GetPlayerFromCharacter(humanoid.Parent)
if not player or timeouts[player] then return end
timeouts[player] = true
-- give them coins...
-- set their timeout to false after 120 seconds
delay(120, function() timeouts[player] = false end)
end)
Have a timeout variable for each player that stores whether or not they're not allowed to have coins. Set this to true and then, after 120 seconds or whatever your desired timeout is, set it to false and they'll be able to collect the coins again.
3 comments
Same thing a wait(120) timeouts[player] = false, no need for this method either
I dont get it.. can you add me on discord = TheScripter75#4442 ( I am really bad at scripting)
@Feahren I feel like the downvote was just pettiness... wait() keeps the function running, delay() schedules a future execution. There's not much of a difference, so why criticize one or the other? I set the debounce, schedule it to be unset later and then forget it, rather than setting it, waiting a bit, then unsetting it later
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
So we can't just use wait(5) then in that case? Are you serious? It even returns how much time actually passed as well! So there's no excuse to use this code. Please, do not.
A. no it doesn't. B. ;)