How to make a loop roblox

(Disclaimer that I don't really know about Roblox, but I think this is a good guess, and I did read a little bit about it.)

Show

When you connect a function to the ended event, it will be called every time the sound ends, not just the next time. After the first time, there will be multiple functions connected to the ended event, which you probably don't want. And, even after the 15th time, the first 14 connections are still there and trying to replay the sound (and playing the sound multiple times at once may also be bad and connecting more and more functions to the event could plausibly cause big problems pretty quickly as the number of connections grows exponentially).

This can probably be fixed by disconnecting each connection the first time it fires. But, it may be easier to just connect one function to the event and have a variable that counts up to 15 that determines whether to refire the sound. Then, if you want to potentially use the sound object again, I guess you should still disconnect the connection.

Untested:

warheadalarmevent.OnClientEvent:Connect(function() script.WarheadAlarm:Play() local n = 0 local connection connection = script.WarheadAlarm.Ended:Connect(function() n = n + 1 if n < 15 then script.WarheadAlarm:Play() else connection:Disconnect() end end) end)

(Note the connection still presumably applies to anything trying to play script.WarheadAlarm, so if multiple things are trying to play this sound at the same time, you may need to further rethink your approach.)

Loops are used for making a script repeat itself. You can make a loop that goes on forever, looking like this:

while true do

wait() --you need this, otherwise roblox will crash

[SCRIPT HERE]

end

But if you want to make a script repeat a certain number of times, you need to add some more detail to your script. If this is you, use this script:

T = 10

for i = 1, T do

[SCRIPT HERE]

end

When it says ' T = 10 ', it is telling the script to repeat 10 times.

When it says ' for i = 1, T do ' it is saying i = 1, and to add 1 to i each time it goes through the script. And when i = T, or 10, it tells the script to go all the way through, and in this case, to end. 

So the script will repeat 10 times, then continue on to where it says ' end ' , and will end the script. 

Next in list --->

The platform should be constantly disappearing and reappearing, with a few seconds between each change. It’s impossible to write an infinite number of function calls — fortunately, with a while loop, you don’t have to.

A while loop runs the code inside it for as long as the statement after while remains true. This particular loop needs to run forever, so the statement should just be true.

  • Create a while true loop at the end of your script.
local platform = script.Parent local function disappear() platform.CanCollide = false platform.Transparency = 1 end local function appear() platform.CanCollide = true platform.Transparency = 0 end while true do end

Toggling the Platform

In the while loop, you’ll need to write code to wait a few seconds between the platform disappearing and reappearing.

The built-in function wait can be used for this. In the parentheses the number of seconds to wait is needed: for example wait(3).

⚠️ Whatever you do, never make a while true loop without including a wait — and don’t test your code before you’ve put one in! If you don’t wait, your game will freeze because Studio will never have a chance to leave the loop and do anything else.

Three seconds is a sensible starting point for the length of time between each platform state.

  1. In the while loop, call the wait function with 3 in the parentheses.

  2. Call the disappear function.

  3. Call the wait function again with 3 in the parentheses.

  4. Call the appear function.

    local function appear() platform.CanCollide = true platform.Transparency = 0 end while true do wait(3) disappear() wait(3) appear() end

The code for the platform is now complete! Test your code now and you should find that the platform disappears after three seconds and reappears three seconds later in a loop.

You could duplicate this platform to cover a wider gap, but you’ll have to change the wait times in each script, otherwise the platforms will all disappear at the same time and players will never be able to cross.

local platform = script.Parent local function disappear() platform.CanCollide = false platform.Transparency = 1 end local function appear() platform.CanCollide = true platform.Transparency = 0 end while true do wait(3) disappear() wait(3) appear() end


5 min

A loop lets you execute code multiple times. Each type of Lua loop repeats a block of code but in different ways.

Loop Types

for

The for loop lets you run a command or group of commands a set number of times. The basic syntax includes a control variable, a start value, an end value, and an optional increment value.

for

count
How to make a loop roblox
control
variable

=

1
How to make a loop roblox
start
value

,

10
How to make a loop roblox
end
value

,

1
How to make a loop roblox
increment
value

do

The following example loop starts at 1 and counts up until 5, printing the value of count (the control variable) on each iteration:

If the optional increment value is included in the statement, a positive value will count up while a negative value will count down:

The while loop evaluates if a condition is true or false. If false, the loop ends and the code following it continues to execute. If true, the code between do and end executes and the true/false condition is reevaluated afterward.

The while loop can also be used for infinite game loops by using simply true as the condition:

Always include a delay such as wait() inside an infinite loop, as omitting it can freeze the game.

A repeat loop repeats until a certain condition is met. Note that the code between repeat and until is executed at least once because the conditional test is performed afterward.

If you’re running a loop that won’t typically end, such as an infinite while true do loop, you can force it to end with the break command so the script can continue running the code following it:

Tags: