Playing music once in a loop

I’ve got a loop that checks if a value is true or not. If that value is true, I’d want it to play a sound effect once.

I understand that there are probably ways how to not use forever lasting loops, but for me, it seems to be the easiest way since I’m like a massive beginner at coding. My code checks if the surfacelight is emitting 10 brightness and then plays the sound effect, but it keeps doing it forever since it’s a forever lasting loop of just checking if it’s still emitting so much light. Is there any way to code around forever loops and just check the value once it is true or false?

The madness I’ve created in lua code block:

local press = false
local TweenService = game:GetService("TweenService")
local soundNotification = Instance.new("Sound")
soundNotification.Parent = game.Workspace
soundNotification.SoundId = "rbxassetid://5153734608"
local playsound = false
 
local part = game.Workspace.Part.SurfaceLight
part.Brightness = 0
 
local goal = {}
goal.Brightness = 10
 
local tweenInfo = TweenInfo.new(3)
 
local lightOn = TweenService:Create(part, tweenInfo, goal)


script.Parent.ImageButton.MouseButton1Down:Connect(function()
	if press == false then press = true else press = false
	end
end)

while true do
	if press == false then game.Workspace.Part.SurfaceLight.Brightness = 0
	end
	if press == true then lightOn:Play()
		if part.Brightness == 10 then lightOn:Pause()
			soundNotification:Play() wait(3) soundNotification:Pause()
		end
	end
	wait(1)
end

And here’s the same thing in a picture:

1 Like

try to destroy the sound? (30 charssssssss)

4 Likes

ooOoo… That could work. But hmm… it might bring up an error.

1 Like

it might error something like “attempt to nil with “:Play()”” or something like that

so like try doing:

if soundNotification ~= nil then
   soundNotification:Play() wait(3) soundNotification:Pause()
end
2 Likes

Amazing. It seems to work. Didn’t even bring up an error, suprisingly.
Thank you so much for your help! C:

1 Like

xD
I’m too dumb to manage to make it shorter.

1 Like

whats the problem or error ((CHARRSS)

1 Like

Once it reached a certaint point, the sound played, you know. But… If I would shut the lights and turn them back on, the sound wouldn’t play since it’s destroyied.

Any way to reuse the sound or make it come without it like repeating forever?

try Instance?

local NewSound = Instance.new("Sound", SomethingParent)
NewSound.SoundId = "rbxassetid://0" -- Sound ID
NewSound:Play()
1 Like

Yeh… That’s what I did.
But the problem is that it’s in the forever loop.

while true do
if press == false then game.Workspace.Part.SurfaceLight.Brightness = 0
end
if press == true then lightOn:Play()
if part.Brightness == 10 then lightOn:Pause()
local soundNotification = Instance.new(“Sound”)
soundNotification.Parent = game.Workspace
soundNotification.SoundId = “rbxassetid://5153734608”
soundNotification:Play() wait(3) soundNotification:Pause()
soundNotification:Destroy()
end
end
wait(1)
end

If I place it outside of it, it’ll get deleted and won’t come back.

sorry im late here.
so heres what i did

local press = false
local TweenService = game:GetService("TweenService")
local playsound = false
 
local On = false

local part = game.Workspace.LightPart.SurfaceLight
part.Brightness = 0
 
local goal = {
	Brightness = 10
}
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
 
local lightOn = TweenService:Create(part, tweenInfo, goal)

script.Parent.ImageButton.MouseButton1Down:Connect(function()
	if press == false then
		press = true
	else
		press = false
	end
end)

while wait(.1) do
	print(press)
	
	if press == false then
		part.Brightness = 0
		On = false
		
	elseif press == true then
		lightOn:Play()
		
		if part.Brightness == 10 and On == false then
			On = true
			
			local soundNotification = Instance.new("Sound")
            soundNotification.Parent = game.Workspace
            soundNotification.SoundId = "rbxassetid://5153734608"
			soundNotification:Play()
			
			game:GetService("Debris"):AddItem(soundNotification, 3)
		end
	end
end

i dont know if thats what your trying to do but thats it. btw i changed it a bit
btw on that line that has:

local part = game.Workspace.LightPart.SurfaceLight

it was Part right? just change it

1 Like

Oh sheet. Lemme view what you just did. xD
About that part, yeah… It’s supposed to be a light. xD Just didn’t want to take time changing the name of it.

1 Like

Omg it looks awesome. xD
Didn’t ever know anything about “Debris” function.

debris makes a object have a certain time of its existence

1 Like

Ur brain is literally massive. I couldn’t do it without you. :slight_smile:

Ik it might be pretty annoying to ask, but do you maybe want to add friends on Roblox since ur a really nice fellow? C:

1 Like

Sure. you can add me. ask me if you need help! im trying to not respond short sentence because i got warning so yea.

1 Like

My god… I’m so god damn dumb that I can’t litereally continue a single bit. I want to die.
Do you have any idea how to control multiple lights at the same time?

for _,v in pairs(game.Workspace.Lights:getDescendants()) do
if v.ClassName == “SurfaceLight” then local part = v print “found” end end

I found smthing like that but I can’t seem to get it work.

1 Like

so tired rn… heres the code:

local press = false
local TweenService = game:GetService("TweenService")
local playsound = false
 
local On = false

local Lights = game.Workspace.LightParts

local function SetEnableLightParts(TrueOrFalse)
	if TrueOrFalse == true then
		for i, v in pairs(Lights:GetDescendants()) do
			if v:IsA("SurfaceLight") then
				v.Brightness = 0
				
				local goal = {
					Brightness = 10
				}
				local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
				local lightOn = TweenService:Create(v, tweenInfo, goal)
				
				lightOn:Play()
			end
		end
		
	else
		
		for i, v in pairs(Lights:GetDescendants()) do
			if v:IsA("SurfaceLight") then
				v.Brightness = 0
			end
		end
	end
end

----------------------------------------------------------

script.Parent.ImageButton.MouseButton1Down:Connect(function()
	if press == false then
		press = true
	else
		press = false
	end
end)

----------------------------------------------------------

while wait(.1) do
	print(press)
	
	if press == false then
		SetEnableLightParts(false)
		On = false
		
	elseif press == true then	
		if On == false then
			On = true
			
			SetEnableLightParts(true)
			
			local soundNotification = Instance.new("Sound")
            soundNotification.Parent = game.Workspace
            soundNotification.SoundId = "rbxassetid://5153734608"
			soundNotification:Play()
			
			game:GetService("Debris"):AddItem(soundNotification, 3)
		end
	end
end

and heres the setup i made

image

1 Like

Oh god… How long did it take for you to like understand the basic knowledge of coding?