I recently encountered a peculiar behavior while working with the BoolValue.Changed:Wait()
function in my code. It seems that the expression repeat task.wait() until IsChasingBoolean.Value
is functioning correctly, but when I attempt to achieve the same functionality using the code snippet if not IsChasingBoolean.Value then IsChasingBoolean.Changed:Wait() end
, it does not yield the expected results.
Let me provide some context to better understand the issue. I have a BoolValue
variable called IsChasingBoolean
, which I use to determine whether an entity in my game is currently engaged in chasing behavior. In certain parts of my code, I want to pause execution until the IsChasingBoolean
value changes to true
.
Initially, I implemented a repeat loop with task.wait()
to continuously check the IsChasingBoolean.Value
until it becomes true
. Surprisingly, this approach works as intended and effectively waits until the value changes before proceeding with the rest of the code.
However, in an effort to optimize my code (the CPU on lower end devices spiked like crazy) and make it more readable, I tried using an if
statement in conjunction with IsChasingBoolean.Changed:Wait()
instead. The idea was to wait for the value change event to occur and then continue executing the code. Unfortunately, this alternative approach did not produce the desired outcome, and the animations for my entity do not play.
I find this behavior rather puzzling since both the repeat task.wait() until IsChasingBoolean.Value
and the if not IsChasingBoolean.Value then IsChasingBoolean.Changed:Wait() end
expressions should theoretically achieve the same result.
Here is my code:
local SCP096 = script.Parent.Parent
local Humanoid = SCP096:WaitForChild("Humanoid")
local Run = Humanoid.Animator:LoadAnimation(SCP096:WaitForChild("Run"))
local AttackRunStart = Humanoid.Animator:LoadAnimation(SCP096:WaitForChild("AttackRunStart"))
local AttackRun = Humanoid.Animator:LoadAnimation(SCP096:WaitForChild("AttackRun"))
Run:AdjustSpeed(2)
AttackRunStart:AdjustSpeed(2)
AttackRun:AdjustSpeed(2)
local IsChasingBoolean = SCP096:WaitForChild("IsChasing")
local CanSeeBoolean = SCP096:WaitForChild("CanSee")
local PlayedAttackRunStart = false
while true do
--// this is too CPU heavy but works repeat task.wait() until IsChasingBoolean.Value
if not IsChasingBoolean.Value then --// new code
IsChasingBoolean.Changed:Wait()
end
print("Chasing") --//Actually prints for some reason
repeat --// this doesn't seem to do anything now
if IsChasingBoolean.Value == true then --// Basically I have a chasing animation for when my entity can see you, and a separate one for when he cannot. That's what this loop is for.
if CanSeeBoolean.Value == false then
AttackRunStart:Stop()
AttackRun:Stop()
Run:Play()
Run:AdjustSpeed(2)
Run.DidLoop:Wait()
elseif CanSeeBoolean.Value == true then
if PlayedAttackRunStart == false then
PlayedAttackRunStart = true
Run:Stop()
AttackRunStart:Play()
AttackRunStart:AdjustSpeed(2)
repeat task.wait() until AttackRunStart.IsPlaying == false
elseif PlayedAttackRunStart == true then
Run:Stop()
AttackRun:Play()
AttackRun:AdjustSpeed(2)
AttackRun.DidLoop:Wait()
end
end
end
task.wait()
until IsChasingBoolean.Value == false
print("hmm") --// never actually prints (it shouldn't), main script disables this one before it can print anything.
Run:Stop()
AttackRunStart:Stop()
AttackRun:Stop()
PlayedAttackRunStart = false
end
Remember, all of this functioned properly previously.
I would greatly appreciate any insights or suggestions on why the better approach fails to function as expected. Perhaps there’s a subtle nuance that I’m overlooking, or there’s a better approach to accomplish the desired functionality. Thank you in advance for your assistance!
TL; DR:
For some reason “repeat task.wait() until IsChasingBoolean.Value” is working but “if not IsChasingBoolean.Value then IsChasingBoolean.Changed:Wait() end” is not or is breaking my code.