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

How can I make my script repeat itself forever?

Asked by 4 years ago
1workspace.CoreC.Temp.Value +=1

2 answers

Log in to vote
0
Answered by 4 years ago

Hey, you could use a while loop or a repeat loop for that :D

While Loop:

1local CoreC = game.Workspace.CoreC.Temp -- Defines your part or whatever
2 
3while true do -- Will run forever
4 
5    CoreC.Value = CoreC.Value + 1 -- Set to how much the value is supposed to be
6    wait(1) -- You want to have some cooldown or the game will crash
7end

Repeat Loop:

1repeat
2 
3    CoreC.Value = CoreC.Value + 1 -- Set to how much the value is supposed to be
4    wait(1) -- You want to have some cooldown or the game will crash
5 
6until MyRepeater == false -- Basicaly runs forever untill MyRepeater is set to false

https://developer.roblox.com/en-us/articles/Loops

Ad
Log in to vote
0
Answered by 4 years ago

There are two ways: One is a while true loop. This one is only needed if you are adding in a wait in between each cycle. An example is if I want a countdown to go down every second. EXAMPLE:

1local countdownTime = 60
2local countdown = countdownTime
3local inc = 1
4while wait(inc) do --Every second this will run
5    countdown -= inc --Setting the countdown variable to itself, but 1 less.
6end

The second is a Run.Stepped function. This is only ran if you want it to run constantly every second. Using a wait() at the end won't work.

If you are on a server script, the only way to use it is like this:

1local run = game:GetService("RunService")
2run.Stepped:Connect(function()
3    --do whatever here.
4end)

But, if you are on a client (local) script, it can be used in two ways.

1local run = game:GetService("RunService")
2run.Stepped:Connect(function()
3    --do whatever here.
4end)
5run.RenderStepped:Connect(function()
6    --do whatever here
7end)

The difference between Stepped and RenderStepped is that RenderStepped is better if you are, for example, making arms that follow your cursor client sided. If you use .Stepped, it'll lag out and glitch, but if you use .RenderStepped, it will look nice and smooth.

Answer this question