How would I make a repeat only last 60 seconds?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I wan’t to make a repeat, until, last 60 seconds only but I don’t know exactly how to.
    I have a simple script but I don’t know how to make it last only 60 seconds.
    Here’s my script if you are wondering.
game.ReplicatedStorage.RandomRemotes.Alien.OnClientEvent:Connect(function()
	repeat
		game.Lighting.ColorCorrection.TintColor = color1
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color2
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color3
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color4
		wait(1)
	until
end)

If anybody could tell me how to make it last 60 seconds it would be really appreciatted.

1 Like

You can use tick(). It doesn’t really matter what it represents, but it increases by 1 every second.

So

  1. When the event fires, store the current tick() somewhere
  2. When the loop starts, check the new tick() and see if its 60 more than the one you stored

Something like:

local startTime
game.ReplicatedStorage.RandomRemotes.Alien.OnClientEvent:Connect(function()
	startTime = tick()
	repeat
		game.Lighting.ColorCorrection.TintColor = color1
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color2
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color3
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color4
		wait(1)
	until tick() - startTime > 60
end)

Note that this will reset the timer if the event gets fired within the 60 seconds.

1 Like
local done = false

coroutine.wrap(function()
game.ReplicatedStorage.RandomRemotes.Alien.OnClientEvent:Connect(function()
done = false
	repeat
		game.Lighting.ColorCorrection.TintColor = color1
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color2
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color3
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color4
		wait(1)
	until done == true
end)
end)()

game.ReplicatedStorage.RandomRemotes.Alien.OnClientEvent:Connect(function()
wait(60)
done = true
end)

Basically, a coroutine allows you to activate other parts of a script at the same time as other parts. Using this, it is possible to create a function that waits 60 seconds until a value is set to true, disabling the repeat.

Since 4 * 15 is equal to 60, you can do something similar to this.


game.ReplicatedStorage.RandomRemotes.Alien.OnClientEvent:Connect(function()
	for i = 1, 15 do
		game.Lighting.ColorCorrection.TintColor = color1
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color2
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color3
		wait(1)
		game.Lighting.ColorCorrection.TintColor = color4
		wait(1)
         end
end)

Not sure if this works since im on mobile.