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.

5 comments
Since you have a while loop, your changed event will never be called as well. Make sure it's before the while loop.
The connect event and the function*
pls mark the question as answered if it worked so other people with the same question can find it more easily.
That worked! I just had to move the connect event and the function up above the loop. Thanks so much!
np i ran into this issue too like, two weeks ago