Still have questions? Join our Discord server and get real time help.
0

# Why isnt this simple sound and light script working??????

I swear roblox pissed me off so f**kin much sometimes. Please help me with this simple script i do not understand at all why its not working. Its making me very angry.

wait()

local Lights = true
local DB = false

local Thread = coroutine.create(function() -- red flashing lights this part works
for i,x in pairs(workspace.Map.Lights:GetDescendants()) do
if x:IsA("PointLight") then
if x.Name == "RLight" then
spawn(function()
while wait() do
x.Enabled = true
repeat wait() x.Brightness = x.Brightness - 0.01 until x.Brightness <= 0
x.Enabled = false
x.Brightness = 0.75
wait(.5)
end
end)
end
end
end
end)

script.Parent.MouseButton1Click:Connect(function() -- works first time i click but after that it doesnt
if DB then return
else
DB = true
for i,v in pairs(workspace.Map.Lights:GetDescendants()) do
if v:IsA("PointLight") and v.Name == "PointLight" then
v.Enabled = not v.Enabled
end
end
if Lights then
Lights = not Lights
game.SoundService.Alarm:Play()
else
Lights = not Lights
game.SoundService.Alarm:Stop()
end
DB = false
end
end)


0
Elyzzia 1023
4 days ago
Edited 4 days ago

you can't yield a coroutine from outside itself, so when you do coroutine.yield(Thread) it actually yields the MouseButton1Click thread

what you can do instead is make it so that if the lights are off, the loop continues running, but just makes it so that the lights have a brightness of 0

wait()
local RunService = game:GetService("RunService")

local Lights = true
local Brightness = 0.75

-- having a ton of different coroutines is kind of extremely nasty, and also less efficient than simply iterating over descendants each frame
-- plus they might be out of sync with each other

RunService.Heartbeat:Connect(function(timeDelta) -- timeDelta is the time passed since the last frame
Brightness -= timeDelta/3
if Brightness <= 0 then
Brightness = 0.75
end
for _, light in pairs(workspace.Map.Lights:GetDescendants()) do
if light:IsA("PointLight") and light.Name == "RLight" then
light.Brightness = Lights and 0 or Brightness -- this behaves similarly to an if statement, so it's like if condition, then use value A, else use value B
-- more specifically it uses the last value it evaluated but it's not really necessary to know that
end
end
end)

script.Parent.MouseButton1Click:Connect(function()
-- there's no reason to have a debounce when the debounce is set back to false immediately
for i,v in pairs(workspace.Map.Lights:GetDescendants()) do
if v:IsA("PointLight") and v.Name == "PointLight" then
v.Enabled = not v.Enabled
end
end
if Lights then
game.SoundService.Alarm:Play()
else
game.SoundService.Alarm:Stop()
end
Lights = not Lights
end)

0
Thank you, i am new to coroutines that is why. XxTGLxX 7 — 4d