-
What do I want to achieve? I want to have this light I made flicker while it’s on according to a sound I have (I know this works so far), but when the light is off it doesn’t change (it just stays off with no properties changed).
-
What is the issue? I think I have multiple issues. I’m running an if statement inside a RunService.Heartbeat function. I did this because I want the light to be constantly updated, but the start (on) or stop (off) functions to run once and have the RunService function just wait until the lightStatus value has changed. The lightStatus value changes based on the light’s switch being flicked on or off, on being true while off being false.
-
What solutions have I tried so far? I tried searching up some solutions but couldn’t really find anything to help me. I always get stuck in a loop inside a loop waiting on each other combo a lot and always get stuck so I thought this would also be a good time to learn how to stop doing that.
This script below in in a local script called “FlickeringLightScript” inside StarterPlayerScripts.
If I need to state any more details I would be more than happy to.
My Code:
local RunService = game:GetService("RunService")
local Light = workspace.Office.Light.Bulb.SpotLight
local Sound = workspace.Office.Light.FlickeringLightSound
local originalAngle = Light.Angle
local originalTransparency = Light.Parent.Transparency
local lightSwitch = workspace.Office:WaitForChild("Lightswitch")
local Switch = lightSwitch:WaitForChild("Switch")
local Main = lightSwitch:WaitForChild("Main")
local lightPrompt = Main:WaitForChild("LightSwitchPrompt")
local lightSound = Main:WaitForChild("LightSwitchSound")
local lightStatus = true
local function start()
Sound.Looped = true
Sound:Play()
Light.Parent.Material = Enum.Material.Neon
Light.Enabled = true
repeat
local currentLoudness = Sound.PlaybackLoudness
Light.Angle = Light.Angle - currentLoudness * 5
Light.Parent.Transparency = Light.Parent.Transparency + currentLoudness / 10
wait(0.05)
Light.Angle = originalAngle
Light.Parent.Transparency = originalTransparency
until lightStatus == false
end
local function stop()
Sound.Looped = false
Sound:Stop()
Light.Parent.Material = Enum.Material.SmoothPlastic
Light.Parent.Transparency = 0
Light.Enabled = false
end
RunService.Heartbeat:Connect(function()
if lightStatus == true then
start()
-- I do not know what to do here.
else
stop()
-- I do not know what to do here.
end
end)
lightPrompt.Triggered:Connect(function()
lightSound:Play()
if lightStatus == true then
lightStatus = false
Switch.Orientation = Vector3.new(55, 180, 180)
lightPrompt.ActionText = "Turn Lights On"
else
lightStatus = true
Switch.Orientation = Vector3.new(35, 0, 0)
lightPrompt.ActionText = "Turn Lights Off"
end
end)
Thank you for your help or even your time reading this!