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

How to make a loop run only once?

Asked by
alibix123 175
8 years ago

This is a pretty hard question to ask because I can't possibly fit everything into a question but I can explain the problem. My game logic/game script is essentially:

function game()
    -- stuff
end

while true do
    while players < 2 do
        -- tell player to invite more players and all that jazz
    end

    if players >= 2 then
        game()
    end
end

(this is just pseudocode, I'm ignoring things like wait() and Roblox API for simplicity because the idea is still the same)

Now, in my 'game' function, when players are ready ( i.e aren't in a menu etc.) it will teleport all the ready players to a point where the game is. Unfortunately, since 'game()' is run all the time, the players keep getting teleported over and over and it doesn't stop. I'm not sure how to make is so that it only ever teleports them once even if 'game()' is constantly run.

Here is the real code for the teleportation explained the best I can:

if #ready >= 2 then -- if the players in the list 'ready' (the players that are ready for the game to start) is less big than or equal to 2
    print(player.Name .. " moved") -- show which player is moved
    player.Character:MoveTo(game.Workspace.LevelGeometry.Model.start.Position - Vector3.new(-10,0,9*offset)) -- actually move the player
end

The code is being looped through all the 'ready' players, and 'game()' is being looped all the time so this means all my players keep getting teleported constantly when I only want them to get teleported once. And honestly, I don't really like having my game script like this but I can't really think of better and more effective methods so if you know other ways to do this it would help! I hope this is understandable ._.

0
Make a conditional statement to keep track of when the players have loaded. When the players are loaded simply use "break" on the loop to stop the loop and move on to the next block of code. AZDev 590 — 8y
0
Like a boolValue in the player or? alibix123 175 — 8y

2 answers

Log in to vote
1
Answered by
Unlimitus 120
8 years ago

First, in your real code, change player.Character:MoveTo() to player.Character.Torso.CFrame. CFrame also returns rotation so it is better to have it now than later.

Second, loops run like: 1, 2, 3, repeat and they do not repeat until they reach the "repeat" or "end" of the while or even a for loop, but events still run because they are only called when something certain happens. So to stop this have the script stall until a certain event happens, which you can write yourself. Here is an example to help give you a better example

function Game()
    -- stuff
end

local deadPlayers = {} -- You will see where we use this later in the code
for _, v in pairs(game:GetService("Players"):GetChildren()) do
    function PlayerDied()
        table.insert(deadPlayers, v)
        print(v.Name.." has died")
    end

    v.Character.Humanoid.Died:connect(PlayerDied) -- when a player dies, add them to the list.
end


while true do
    if #players < 2 then
        -- Tell players to wait until there are more players
    else
        Game() -- run the game
        repeat wait() until #deadPlayers == game:GetService("Players").NumPlayers -- wait until the amount of dead players is equal to the amount of players
        deadPlayers = {} -- Clear the dead players list
        print("Game Over!")
    end
    wait()
end

I haven't tested this code, but it should give you a good idea on how to fix your problem. Hope this helps!

0
Ah! So when they get teleported to say, a race, I could have a 'repeat wait() until race ended' sort of thing? alibix123 175 — 8y
1
Definetely! You can also add a wait after the repeat loop so that you can display some sort of message like "Player1 is the winner." Unlimitus 120 — 8y
Ad
Log in to vote
1
Answered by 8 years ago

Have you tried a repeat until loop Or a for loop?

0
I've tried using a for loop but it still gets repeated since the function 'game()' is repeated alibix123 175 — 8y

Answer this question