How to wait untill a variable is true

Hi again. But I need to know how to make a “wait until a variable is true” script for my Intermission.

7 Likes

You could do something like this:

repeat
   wait()
   --insert script here
until variable == true
8 Likes
while variable == false do

end
4 Likes
repeat wait() until var

Shortest way possible

14 Likes

You could also do something like

while var == false do
   wait()
  --code
end

In here you can set the var to true so that the while loop stops

1 Like

Here is my code:

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
while IntermissionOver == true do
wait()
if player.Team == TeamBlue then
RedTeamGunP.Parent = player.Backpack
else if player.Team == TeamRed then
BlueTeamGunP.Parent = player.Backpack
end
end
end
end)
end)

1 Like

It won’t let me use “Preformeratted text”, I don’t know why. I think it’s a bug.

You can do it like this

-- put ```lua at the beginning of the code

-- code goes here

-- put ``` at the end of the code

Nothing has worked so far. If this helps I am trying to work around the “DisplayScript” of a Roblox template. Every thing seems more complicated though with the Intermission that is in the “DisplayScript”.

At a high level, what are you trying to do?

I think you might’ve asked about your solution without asking about the actual problem first :slight_smile:

2 Likes

I am trying to make it so that after a variable is equal to true, then give one weapon to one team and another to the other. And all these scripts that everyone has been giving me work on different games, but on this game they don’t. I think it has something to do with the intermission in the “DisplayScript” from the Roblox template I am using but don’t know where. There are also no errors popping up in the output bar. It just won’t give the player the weapon.

The best practice would be to not use loops or repeat. You could use a callback function which would be more efficient than waiting for a variable to be true

2 Likes

Can you show the code you use to change that variable?

Or find the parts of the Roblox script that does?

It will be easier to find that and just add a function call than it will be to detect changes of a variable.

Dont use wait, wait is evil.

If you have an external thread controlling your variable, use a bindable event and wait on that to fire.

(or you could use coroutines but thats a topic for another day)

And you haven’t posted any of your code so we have no idea what or where you need help, you cant just ask us a question without providing code to go along with it.

Context helps.

3 Likes

The best method for this is to use a value instead of a variable. You can get an event for when it is changed. For example:

Value.Changed:Wait()

6 Likes

This post covers many of the reasons why not to use wait().

A shortened version is basically that your current code is:

local var = false

someAsyncThing(function()
    -- your code here
    var = true
end)

while var == false do
    wait()
end

In this example, the constant waiting uses up processing power, and in my experiments, wait() is approximately a 2-frame or 1/30 second delay. This means that the code could still be waiting even when var has just been set to true.

local event = Instance.new("BindableEvent")
local var = false

someAsyncThing(function()
    -- your code here
    var == true
    event:Fire()
end)

if var == false do -- just in case someAsyncThing is ran before we start waiting for the event.
    event.Event:Wait() -- continues the instant that the event is fired
end

In this example, you can use bindableEvents or remoteEvents so that the code continues practically the instant that the event is fired, making it much quicker. But in a case that someAsyncThing is ran before we start waiting for the event, the if statement preceding Event:Wait() can detect if the function has already run, just like in the first example, but with much less looping.

Please correct me if I’m wrong, as I don’t have a huge understanding of any of this.

2 Likes

This resource may help you:

I find it easy and helpful and better than what you asked for.

otherwise, you would have to do the

repeat wait() until value
1 Like
repeat wait()
until Value == true
print("Value is true")