"Repeat wait() until" does not work

EDIT: I figured it out and it works now

Old: It works at the very start of a different script and this script works perfectly without it. I’ve tried tons of different ways of writing it but it never works. The bool (CheckIfPlayerIsInArea2) is definitely changing to true but it still doesn’t proceed with the rest of the script.

getRandomPlayer()
	CreateDialogueEvent:FireAllClients(getPlayerImage(randomPlayerId),"Let's split up to cover more ground.")
	wait(5)
	repeat wait(0.05) until game.StarterPlayer.StarterCharacterScripts.CheckIfPlayerIsInArea2.Value == true
	CreateDialogueEvent:FireAllClients(getPlayerImage(randomPlayerId),"it worked")

this is the entire output:

true  -  Server - check if player in area:5
  00:30:28.533  CheckIfPlayerIsInArea is not a valid member of Model "Workspace.tutorialMonster"  -  Server - check if player in area:2
  00:30:28.534  Stack Begin  -  Studio
  00:30:28.534  Script 'Workspace.Area.check if player in area', Line 2  -  Studio - check if player in area:2
  00:30:28.534  Stack End  -  Studio
  00:30:28.942  CheckIfPlayerIsInArea is not a valid member of Model "Workspace.tutorialMonster"  -  Server - check if player in area:2
  00:30:28.942  Stack Begin  -  Studio
  00:30:28.942  Script 'Workspace.Area.check if player in area', Line 2  -  Studio - check if player in area:2
  00:30:28.942  Stack End  -  Studio
  00:30:33.541  Infinite yield possible on 'Workspace.tutorialMonster:WaitForChild("Humanoid")'  -  Studio
  00:30:33.542  Stack Begin  -  Studio
  00:30:33.542  Script 'Workspace.tutorialMonster.Health', Line 6  -  Studio - Health:6
  00:30:33.542  Stack End  -  Studio

The only thing I can think of is the tutorialMonster is somehow affecting this script.

1 Like

You need to check in the player’s character, not his startercharacterscripts.

the bool is located inside startercharacterscripts

Instead of
game.StarterPlayer.StarterCharacterScripts.CheckIfPlayerIsInArea2
do
game.Players.LocalPlayer.Character.CheckIfPlayerIsInArea2.Value == true

Shouldn’t it go inside the player’s character when the player spawns ?

It’s not working because you have an error. The error stops the code.

The error is from “CheckIfPlayerIsInArea” not being in the “tutorialMonster”.

No, that is correct, I had the same idea but what goes into StarterPlayer is just information for you game to put into players.

Anything inside of StarterCharacterScripts will replicate to the character once the start of a game.

Your error is saying that your value you are trying to reference is not apart of the Model “TutorialMonster” in the workspace.

These two lines of code are essentially the same thing. If something exists, it should evaluate to true.

For example, I can do this code:

local player --equal to player
if player.Character then
    --code
end

This code checks if the player exists by converting it to a true/false value. If the player exists, it’s true. If it’s not, it’s false (nil → boolean is false).

What? That had nothing to do with what I said to rewrite the line


game.StarterPlayer.StarterCharacterScripts.CheckIfPlayerIsInArea2.Value == true


into


game.Players.LocalPlayer.Character.CheckIfPlayerIsInArea2.Value == true


Because the first one only checks the starterplayer which will affect, not the player, but the starterplayer. That’s all it will do.
The one I said to change it to checks INSIDE the actual player.

I don’t see how that code would work because you can’t get the localplayer in a serverscript, and your player value is nil, so you’re basically doing if nil.Character == true which doesn’t make any sense.

To get a player in ServerScripts, I would either use the playerAdded event and then use the “player” parameter, or loop through the Players service and check if v == to someone’s username.

This won’t work in a ServerScript because you cannot reference the LocalPlayer in a ServerScript.

Edit: Also since the OP is using FireAllClients(), he probably doesn’t wanna switch his entire system over to LocalScripts.

Oops! My bad. I thought you were trying to say to switch a boolean thing to an == operation.

I think the code is server sided, because they use FireAllClients.


OP:
There are two possible problems:

  • You use the instance before it exists. In this case, use WaitForChild before hand.
  • Your instance will never be there, and you’re looking in the wrong place. In this case, you’ll need to change your code to reference the correct object.

OH it’s a serverscript, i just assumed it was a localscript because I use those more.

1 Like

repeat wait() until shouldn’t be used for something like this, I get it seems simple but using wait() can really slow down your game.

If you need to detect a value change, use .Changed

Instead of:

repeat
  wait()
until Value2.Value == true

Do:

local HasChanged = false
Value2.Changed:Connect(function(Property)
  if Property == "Value" then
    if Value2.Value == true and not HasChanged then
      HasChanged = true
      -- Do code here
    end
  end
end)

Alternatively, you could use :GetPropertyChangedSignal() but either way works.

1 Like

Instances inheriting from ValueBase each have special Changed events that only fire when their value changes so it’s functionally equivalent to using GetPropertyChangedSignal for other instances. The Changed events will also fire with the new value which is more convenient if you want to check directly. In the context of an xValue object, use Changed.

value2.Changed:Connect(function (newValue)
    -- Do things here. Changed only fires when the Value is changed.
end)

Ohh okay, I didn’t realise that the changed only fires for the value, thank you!

Instead of making a new thread about the same issue, check the answers in this one.

Try doing

while task.wait(0.05) do
    if game.StarterPlayer.StarterCharacterScripts.CheckIfPlayerIsInArea2.Value == true then
        break
    end
end

Does this work?

the other thread for the same problem was made before this one but only got 1 answer that didnt work