I tried to use conditionals to break a while true do loop

hey, I tried to use conditionals to break a while true do loop and I was wondering if anyone could help
My Program:

local PhoneCall = math.random(1, 2)
local enabled = true

if PhoneCall == 2 then
    print("Phone Triggered")
    script.Parent.Enabled = true
    while enabled do
        script.Parent.Parent.Phone:Play()
        wait(0.5)
        script.Parent.Parent.Phone:Play()
        wait(1)
    end
else
    print("Phone Not Triggered")
end

script.Parent.Triggered:Connect(function(plr)
    enabled = false
end

any help is amazing
Thx :smiley:

1 Like

One thing I noticed about your code is that when PhoneCall is equal to 2 you set script.Parent.Enabled to true even though enabled is already a local variable. Perhaps this is your error?

Edit: Yeah I’m pretty sure that the error is instead of checking and changing script.Parent.Enabled you change a local variable.

I’m guessing the parent is a ProximityPrompt and it seems you forgot to set enabled to true?

local PhoneCall = math.random(1, 2)
local enabled = false

if PhoneCall == 2 then
    print("Phone Triggered")
    script.Parent.Enabled = true
    enabled = true -- this?

    while enabled do -- while script.Parent.Enabled do
        script.Parent.Parent.Phone:Play()
        wait(0.5)
        script.Parent.Parent.Phone:Play()
        wait(1)
    end
else
    print("Phone Not Triggered")
end

script.Parent.Triggered:Connect(function(plr)
    enabled = false
    -- script.Parent.Enabled = false
end

I didn’t quit understand you, but to break a loop use the word break

Try this out, does it print?

script.Parent.Triggered:Connect(function(plr)
    print("triggered")
    enabled = false
end

script. parent is the proximity prompt

Ok I see people are getting confused about what “Script. parent” is and how it’s different from “Enabled = true”

script.parent is the proximity prompt, I’m setting the proximity prompt to enabled
Enabled = true is unrelated from the proximity prompt

1 Like