Boolean changed event bypasses value check

I’m working a boolean changed event to check if the boolean value is true and then proceed to next function. The current issue I’m encountering is that sometimes the function will even run when the boolean value is set to false.

isValue.Changed:Connect(function()
     if isValue.Value == true then
          print(isValue.Value)
          if isOtherValue.Value == true then
                 print(isValue.Value)
                 print("run function")
                 ---Function---
          end
     end
end)

It should print this if everything run successfully

true
true
"run function"

and strangely sometimes it prints

false
"run function"

One workaround at the moment is that adding one more value check before the function

isValue.Changed:Connect(function()
     if isValue.Value == true then
          print(isValue.Value)
          if isOtherValue.Value == true then
                 if isValue.Value == true then
                      print(isValue.Value)
                      print("run function")
                      ---Function---
                end
          end
     end
end)

It’s the first time having such issue and usually all other boolean changed event is functioning perfectly :raised_hands: Thank you for reading this!

1 Like

mind if i ask what is the logic behind turning booleans to true and false? this is quite strange

1 Like

they are boolean values used for defining states of NPC pathfinding status :raised_hands:

the first boolean is isFinished to see if pathfinding is finished and the second boolean is isChasing to see if NPC is chasing a players or not

The changed event is to check if NPC finished pathfinding and if NPC is chasing a player or not - to proceed the next function

Not sure if there is chance that the boolean is changed so quick and it somehow bypass the first “print” and proceed to next conditional statement? :raised_hands:

1 Like

there is a small chance, this happened to me when i made a while true loop and was testing it with alot of boolvalues, somehow its like a gate,

gate 1 2 3 4 5

each 0.01 seconds using task.wait() i turned true or false all gates

my best attempt was doing
` if gate 1 == true and gate 2 == true and gate 3 == true and gate 4 == true and gate 5 == true then

instead of checking like a filter, i just started checking everything together, this worked 100%, dind’t saw until now any errors or bypassing

when i said ‘‘Filter’’ method is smthing like this:

if npc1 == true then
if npc2 == true then
if npc3 == false then
if npc4 == true then
end

depending on the timing that these values are turned TRUE or False, it will cause weird behavior and it will still pass while the function is running ( Mainly if you place wait() or task.wait() between those true and false if statements

1 Like

THis is just some Small Things, Feel free to ignore it or if it helps,

You can Instead do this check, which involves using the Argument within Changed
you can also shorten boolean == true to just boolean like this:

if boolean then -- is boolean is true

else -- if false or nil

end

So you should be checking if that said value is true, But it would Depend if you keep everything the same, If you want to detect a Specific Property Change, you can use GetPropertyChangedSignal, but you can just use Changed

isValue.Changed:Connect(function(boolean) -- Argument in Changed is used for when a Property Changes
    if boolean then -- if Boolean is true
        if isOtherValue.Value then -- if other value is true
            -- code
        end
    end
end)

It may or may not help with what you’re doing so ¯\_(ツ)_/¯

1 Like

Thank youu all so much for the helps! :raised_hands: and from today I just realised that I’ve overlooked one of the function before the event and there was a “little” task.wait before everything and caused such behavior - once I have added a “return” after the task.wait function to stop it be proceed further and all seems to run perfectly now :bowing_man:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.