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
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?
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
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
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.
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.
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