Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why isn't my script setting a BoolValue correctly?

Asked by 4 years ago

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.

1 answer

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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.

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

Answer this question