I have a script that is supposed to set some variables and such, it's pretty simple. Except when I get to the part of the code that it supposed to change a BoolValue, then it for some reason doesn't wanna work.
local player = game.Players.LocalPlayer local character = player.Character local m = workspace.Robot.HumanoidRootPart local beat = script:WaitForChild("Heartbeat") local chase = script:WaitForChild("Chase") local playChase = script.PlayChase playChase.Value = false local cam = workspace.CurrentCamera beat.PlaybackSpeed = 0.8 beat.Looped = true beat.Volume = 1 beat:Play() player.CameraMode = Enum.CameraMode.LockFirstPerson while true do if character then local rp = character.HumanoidRootPart local distance = (m.Position - rp.Position).magnitude if rp and distance < 40 then -- The npc is near playChase.Value = true beat.PlaybackSpeed = 1 beat.Volume = 1.5 print("Near") elseif rp and distance > 40 then -- You're safe print("Far") playChase.Value = false beat.PlaybackSpeed = 0.8 beat.Volume = 1 end end wait(.1) end local function ChaseIsOn() if playChase then -- If playChase is true then play the chase track -- chase:Play() else -- if playChase is not true then we hold off till later chase:Stop() end end playChase.Changed:Connect(ChaseIsOn)
It's on lines 25 and 32. For some reason it doesn't want to set the variables, yet everything else works as intended. The "ChaseIsOn" function never fires, and when I test it in studio, the BoolValue never changes. I still see the console print "Near" as it should which is also throwing off.
Is it something quick and super easy to fix, or am I overlooking something? I think it may have something to do with the fact that it's in a while loop and it's continually running over and over, but I don't understand why that would be a problem. Can anyone figure this out? Any help is appreciated.
Might be your if statement on line 41. You're comparing if the value objects exists, not it's value. Make it
if(playChase.Value)
Also, make sure your changed event and it's connected function are before the while loop as the changed event will not register since the loop is running.