If loop not detecting bool value change

I have a loop what is meant to detect if a bool value is either true of false. The loop always detects it as false although when I look in the games explorer while the game runs the bool value is positive. The false if statement continues to run and never switches over to the true if statement. Both this script and the script that changes the bool value are server side.

while true do
	wait(1)
		if game.Workspace:WaitForChild("boolvalue").Value == false then
              print("not running") -- this continues to be printed although in the explorer the bool value is true
		else
              print("running")
		end
end

Is there a solution to this?

2 Likes

Hmm, maybe I misunderstand what you mean.
Oh, maybe the time that the boolvalue changes and detecting the boolValue does not match. So, maybe it’s timing problem.

1 Like

Are you changing the value of BoolValue from a client or from server?

Use :GetPropertyChangedSignal as it fires when the given value changes. A while loop is obviously not efficient in this case.

workspace.boolvalue:GetPropertyChangedSignal("Value"):Connect(function()
         if workspace.boolvalue.Value then -- if it's true
           print(workspace.boolvalue.Value)     
      else -- If it's false
           print(workspace.boolvalue.Value)     
     end
end)

This doesn’t indicate that using a while loop will not work but it isn’t efficient to detect changes if there is a given event for it.

What you’re doing is changing it on the client which doesn’t replicate to the server.

To change it on the server, click on Current Client and then change it.

image

1 Like

Is your Script a ServerScript?

The script is a regular script in the workspace so I’m assuming it’s not from the client

1 Like

Make sure you made all the changes on the Server.

I know the property changed signal could work for this situation, but I actually want the condition to be checked every second and not just whenever the bool value changed.

For example, if the bool changed from false to positive or vise versa every minute, I would want either “running” or “not running” printed 60 times.

Also, I’m not changing the bool value manually, I’m just watching it change through the explorer. Its being changed by a regular script in the workspace.

1 Like

Should I move the script that changes the bool to ServerScript?

1 Like

No. It should not effect anything necessary for the case, as from what you tell, this should not happen.

You may be changing it through the explorer on the client, are you sure you change it on the server?

If so, what script changes it? A local, module or a normal one?

1 Like

The script is a normal script which is within the workspace.

while wait(1) do
       local boolvalue = workspace.boolvalue
        boolvalue.Changed:Connect(function()

         if boolvalue.Value then -- true
            print("True")
       else
            print("False")
        end
   end)
end

Your best bet.

-- LocalScript @ StarterPlayerScripts, for it to be able to detect client-sided changes
local obj = workspace:WaitForChild("boolvalue", 10)

repeat
    print(obj.Value and "running" or "not running (v == false)")
    wait(1) -- I'll assume you know this isn't very accurate, regardless
until nil

This would theoretically set up infinite listeners, you don’t want that.

1 Like

the problem seems to be outside of the detecting script, your value changing script might be at fault somehow or there are more than one object named boolvalue in workspace, I replicated it in an empty baseplate and worked fine boolvalue test.rbxl (18.3 KB)