Boolean value script not working

here’s a script i’ve been trying to make it work for hours i’ve tried changing the value type and i found out that it is only works when my game is starting and the boolean value was already set to true but if i try to set the boolean to true with a script it is doesn’t work , i’ve tried to put the code in a local script and a normal script and it is still doesn’t work, help i have no clue (the script is in player gui to make an effect with the gui’s later to a local player).

–and btw sorry for my grammar its because English is my secondary language…

enabled = true

while enabled do
if game.Workspace.Effects.Panic.Value == true then
script.panic:Play() -----a sound for my game
wait(6)

end
end

1 Like

Does enabled ever get set to false in your script or is it just the other one that’s being set to false?

i think i messed up there becasue the enabled is when the script will be enabled and disabled but i removed it

but i tried that instead so if the boolean value gets to false the script will stop ?

while true do
if game.Workspace.Effects.Panic.Value == true then
script.panic:Play()
wait(6)

end
–script.ok.Value = true
end

If you are running stuff on the client(the player) it should always be ran from a LocalScript instead of a script. Scrips should only ever be used for server sided things(e.g: in ServerScriptService)


I think the reason why your script isn’t working is because the while loop doesn’t resume running when you change the enabled value back to true. Conditional loops only run when the value is true to the value you are checking in the loop and when the value changes the loop wont run again.

You should also try and avoid using loops in your code because of the drawbacks they bring. Instead you should always use events if there is an event for what you are trying to do. For example if you want to check if a value has changed, instead of using a infinite loop you should use the changed or GetPropertyChangedSignal events. This community tutorial may be of interest to you as it touched on using events instead of loops:

I am unsure exactly what you are trying to achieve with your code so the code below may not be right for you use case. Assuming you want the panic sound to play whenever the panic value changes to true you could use this code below:

local Panic = workspace.Effects.Panic

Panic:GetPropertyChangedSignal("Value"):Connect(function() -- Fires whenever the panic value changes
	if Panic.Value == true then
		script.panic:Play()
	end
end)

The GetPropertyChangedSignal in the code above will fire each time the panic value in workspace changes. Then the value will be check to see if it is actually true and if it is the panic sound will play.

3 Likes

omg thank you so much :grin::ok_hand::+1: without your help i would have never figured that out :+1:

1 Like