Trouble with script to play audio

Hi, I’m having an issue playing sounds when a certain value is reached. The script looks fine to me, but the sounds do not play in game (they aren’t sounds that Roblox purged). Maybe something else is messing it up?

Simplified the script for less clutter

local fail = game.Workspace:WaitForChild("Values"):WaitForChild("FAIL COOLANT")
local screen = script.Parent
local b1 = script.Parent.Buttons.Button1

while true do
	wait(1)
	if fail.Value >= 600 then
		b1.Cool.Disabled = true
		b1.Transparency = 1
		script.Parent.SurfaceGui.TextLabel.TextTransparency = 1
	else
		b1.Cool.Disabled = false
		b1.Transparency = 0
		script.Parent.SurfaceGui.TextLabel.TextTransparency = 0
	end
end

if fail.Value >= 600 then
	screen.Power:Play()
end

if fail.Value == 0 then
	screen.Startup:Play()
end

I am pretty sure this is because you are trying to play the sounds outside of the while loop, which won’t actually run since the while loop is continuously running without letting the code run below.

Try playing the sound inside the while loop, or just break the while loop so the rest of the code can run below.

Playing sound inside the while loop:

local fail = game.Workspace:WaitForChild("Values"):WaitForChild("FAIL COOLANT")
local screen = script.Parent
local b1 = script.Parent.Buttons.Button1

while true do
	wait(1)
	if fail.Value >= 600 then
		b1.Cool.Disabled = true
		b1.Transparency = 1
		script.Parent.SurfaceGui.TextLabel.TextTransparency = 1
		screen.Power:Play()
	else
		b1.Cool.Disabled = false
		b1.Transparency = 0
		script.Parent.SurfaceGui.TextLabel.TextTransparency = 0

		if fail.Value == 0 then
			screen.Startup:Play()
		end
	end
end

The while loop is precisely why I have the if then code below it. When I first wrote the script, I had both audios in the while true section, which in-game caused the sounds to continuously play instead of only triggering once as the value is hit.
I’m not sure I can break the while true loop because it’s keeping the console off/on until the reboot is activated.
Is there a way to make the while and if loops behave with each other? Or can I set up something different to make the sounds play by an IntValue?

If you want the if statement to run below the while loop, you will have to break the loop in order for that to run. However, if the given condition is not valid, then the sound will not play.

But I honestly don’t think you need a while loop for this, as there are other events you can use to detect Value changes such as Value.Changed or Value:GetPropertyChangedSignal(). Unless you are using the while loop for other things, I highly suggest you use those 2 events.

Example of how you can use those events:

local fail = game.Workspace:WaitForChild("Values"):WaitForChild("FAIL COOLANT")
local screen = script.Parent
local b1 = script.Parent.Buttons.Button1

fail.Changed:Connect(function()
	if fail.Value >= 600 then
		b1.Cool.Disabled = true
		b1.Transparency = 1
		script.Parent.SurfaceGui.TextLabel.TextTransparency = 1
		screen.Power:Play()
	else
		b1.Cool.Disabled = false
		b1.Transparency = 0
		script.Parent.SurfaceGui.TextLabel.TextTransparency = 0

		if fail.Value == 0 then
			screen.Startup:Play()
		end
	end
end)

If you still want to use the while loop, you just want to break the loop for the if statements to run.

local fail = game.Workspace:WaitForChild("Values"):WaitForChild("FAIL COOLANT")
local screen = script.Parent
local b1 = script.Parent.Buttons.Button1

while true do
	wait(1)
	if fail.Value >= 600 then
		b1.Cool.Disabled = true
		b1.Transparency = 1
		script.Parent.SurfaceGui.TextLabel.TextTransparency = 1
		break
	else
		b1.Cool.Disabled = false
		b1.Transparency = 0
		script.Parent.SurfaceGui.TextLabel.TextTransparency = 0
		break
	end
end

if fail.Value >= 600 then
	screen.Power:Play()
end

if fail.Value == 0 then
	screen.Startup:Play()
end

Tried both, the first one with Value.Changed works excellently
You just solved a problem I’ve been stuck on for hours. I’ve only got the beginning basics of Lua down, and I’m still exploring the various “phrases” you can use. I’ll try to learn more than just the words if and while from now on.
O, the joys of being a new programmer…

1 Like