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.
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.