'Script isn't detecting a change with a BoolValue (Roblox Studio)

I'm trying to make a procedurally generating game (ignore the goal of the game).

Currently I'm trying to make it so when you walk over a grid piece and collide with an invisible part it makes that specific grid piece the Current Global Object (CGO).

Using a BoolValue I made it possible to use this as some form of global variable, but whenever I try to use said value it only detects it as false even when it is indeed showing as true in the Explorer. It only works when I set the value to true BEFORE testing the game.

Here's the code in the script that's meant to detect the value:

it's a regular script and not a LocalScript fyi

local CGO = "0,0"
local tracker = game.Workspace.Tracker00
--local CGOa = tracker.CGOa

local trackerPos = tracker.Position
local trackerX = trackerPos.X
local trackerY = trackerPos.Y
local trackerZ = trackerPos.Z

while true do

    while tracker.CGOa.Value == false do
        tracker.YVal.Value = 7
        print("Set TrackerYVal to 7")
        wait()
        if tracker.CGOa.Value == true then
            break
        end
    end

    while tracker.CGOa.Value == true do
        tracker.YVal.Value = 14
        print("Set TrackerYVal to 14")
        wait()
    end

    tracker.CFrame = CFrame.new(trackerX, trackerY, trackerZ)
    wait()

end

Any help would be much appreciated.



Solution 1:[1]

Rather than using infinite while loops, consider listening for the Changed signal. It's possible that the break command might be escaping both loops.

local tracker = game.Workspace.Tracker00

tracker.CGOa.Changed:Connect(function(newVal)
    print("CGOa changed to ", newVal)
    if newVal then
        tracker.YVal.Value = 14
        print("Set TrackerYVal to 14")
    else
        tracker.YVal.Value = 7
        print("Set TrackerYVal to 7")
    end

    -- update the tracker position based on newly updated values
    local x = tracker.XVal.Value
    local y = tracker.YVal.Value
    local x = tracker.ZVal.Value
    tracker.CFrame = CFrame.new(Vector3.new(x, y, z))
end)

I made some assumptions about how you were positioning the tracker, because the old code would reset its position after every loop.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kylaaa