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

how do I make a script work for 120 seconds per person?

Asked by 5 years ago

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.

2 answers

Log in to vote
-1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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!

0
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. TerrodactyI 173 — 5y
0
A. no it doesn't. B. ;) Ziffixture 6913 — 5y
Ad
Log in to vote
-1
Answered by 5 years ago

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.

0
Same thing a wait(120) timeouts[player] = false, no need for this method either Ziffixture 6913 — 5y
0
I dont get it.. can you add me on discord = TheScripter75#4442 ( I am really bad at scripting) Newar8519 0 — 5y
0
@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 TerrodactyI 173 — 5y

Answer this question