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

How do you wait for a value to become true?

Asked by 5 years ago

Hello there,

How do you wait until a boolean value become true? I'm not sure how to do this. Help is very much appreciated!

local InventoryCheck = false

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
    --- When the value is true do
        LoadInventory(player)
        print(player.Name .. "'s inventory has been loaded.")
    end)
end)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Both answers here should not be used. This is not good practice or how you should be coding. Just because something will work does not mean it should be used.

All value objects contain a Changed event. This event passes the new value of the object which is why it should be used. GetPropertyChangedSignal is a function that passes back a generic event. This event builds on the existing event called Changed which is part of the base class Instance.

You should use the correct event for the task. This means using the event which is part of the value object.

## Back to your question

You are asking how do you load a players inventory when the variable InventoryCheck is set to true.

There are multiple solutions to this as you have not explained when you want to load the players inventory. This is why it is key to explain how you want your code to work as it may be a XY problem(what you think you need may not be what you really need).

To list some solutions:- - You can choose when to spawn the player in meaning you do not even need a boolean value. how-to-prevent-a-player-from-spawning

  • Using a table to store a list of player that need the inventory loaded. Then simply loop through the table loading the players inventory. This will replace the need to use any boolean values. (Using a ModuleScript might help)

  • Using a boolean value object to call LoadInventory when set with a lot of limitations. Using boolValue.Changed:Wait() would not be a solution here as it can cause an infinite yield. Also you would need to cleanup the boolean value correctly when it is no longer needed.

  • Using a bindable event/function or a custom event system but this will have similar issues as using a boolean value. You would need to correctly remove the event/function if the player leaves the game.

  • There are probably others....

Only you can decide which method with be best for your needs. Juse because one method works does not make is solution for your needs.

### Quick example of redundant events

This example shows some possible problems with some methods. Using a BooleanValue.

```lua local InventoryCheck = Instance.new("BooleanValue")

game:GetService("Players").PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) -- ect... end)

-- example to load the player inventory
InventoryCheck.Changed:Connect(function(newValue)
    LoadInventory(player)
    print(player.Name .. "'s inventory has been loaded.")       
end)

end)

wait(5) -- players join ext InventoryCheck.Value = not InventoryCheck.Value -- to fire the changed event the value must be different

wait(5)-- some players leave

-- error the Changed event for the players that left the game will still be used -- this will cause you some problems InventoryCheck.Value = not InventoryCheck.Value ``` As you can see in the above code now you are stuck with functions thate will run upon Changed but with players that are not in the game.

There are solution to solving this problem but again without knowing how you want this to work we are unable to help you.

## Tips

  • When coding use events where possible. Do not use loops.
  • Use loops with care. Check that the loop will/can end if a player leaves the game.
  • Check if there are other solutions that can solve your problem do not use the first thing that works. There may be better solutions available.
  • Check the wiki.

I hope this helps. Please comment if you have any other questions.

Ad

Answer this question