What do you want to achieve? Im trying to make it so when the player clicks the switch it turns on or off all Lights in the Lights Folder!
What is the issue? For some reason its just cycling through all the lights and the switch is flipin back and foward it does one light at a time…
What it looks like rn… vVv https://i.gyazo.com/cd6f8ad4492e09ff45a8a51e396262c0.mp4
What solutions have you tried so far? I Used a For loop and put all light models in the smae folder… vVv
LightSwitch Script… vVv
local tweenservice = game:GetService("TweenService")
local SwitchInfo = TweenInfo.new()
local SwitchModel = script.Parent
local SwitchHinge = SwitchModel.PrimaryPart
local Switch = SwitchModel.Switchlever
local Lightbulb = SwitchModel.Parent.Parent.Lights
local Lights = SwitchModel.Parent.Parent:WaitForChild("Lights"):GetChildren();
local LightsOn = tweenservice:Create(SwitchHinge, SwitchInfo, {
CFrame = SwitchHinge.CFrame * CFrame.Angles(0, 0, math.rad(-300))
})
local LightsOff = tweenservice:Create(SwitchHinge, SwitchInfo, {
CFrame = SwitchHinge.CFrame * CFrame.Angles(0, 0, math.rad(0))
})
local ClickSwitch = Instance.new("ClickDetector")
local Clicked = false
ClickSwitch.Parent = Switch
ClickSwitch.Name = "ClickSwitch"
local function light()
for _, Light in pairs(Lights) do
if Light:FindFirstChild("LightPart") then
if not Clicked then
local PLight = Light.LightPart.Light
LightsOn:Play()
wait(0.3)
SwitchModel["FlipSwitchON SFX"]:Play()
Light.LightPart.Material = Enum.Material.Neon
Light.LightPart.BrickColor = BrickColor.new("White")
PLight.Enabled = true
Clicked = true
wait(0.2)
print(Lights)
else
local PLight = Light.LightPart.Light
LightsOff:Play()
wait(0.3)
SwitchModel["FlipSwitchOFF SFX"]:Play()
Light.LightPart.Material = Enum.Material.SmoothPlastic
Light.LightPart.BrickColor = BrickColor.new("Ghost grey")
PLight.Enabled = false
Clicked = false
wait(0.2)
end
end
end
end
ClickSwitch.MouseClick:Connect(light)
I’m not 100 sure but the wait(0.3) might be messing with your lights, since the variable Clicked isnt instantly made true, which then runs the else code, try to move the Clicked = true to the top under if not clicked then and same by the else statement putting Clicked = false right under the else
The wait will yield and cause the loop to not get to the next light for that amount of time. You need to wrap the inside of the for loop (after the if statements) with task.spawn to make them run independantly.
I put Task.spawn under the if statment and the else but when I put Light or Lights in Task.Spawn() it does nothing but when i put in light which is the name of the function it switches between lights then lags and crashes…
local function light()
for _, Light in pairs(Lights) do
if Light:FindFirstChild("LightPart") then
if not Clicked then
task.spawn(Light)
Clicked = true
local PLight = Light.LightPart.Light
LightsOn:Play()
wait(0.3)
SwitchModel["FlipSwitchON SFX"]:Play()
Light.LightPart.Material = Enum.Material.Neon
Light.LightPart.BrickColor = BrickColor.new("White")
PLight.Enabled = true
wait(0.2)
print(Lights)
else
task.spawn(Light)
Clicked = false
local PLight = Light.LightPart.Light
LightsOff:Play()
wait(0.3)
SwitchModel["FlipSwitchOFF SFX"]:Play()
Light.LightPart.Material = Enum.Material.SmoothPlastic
Light.LightPart.BrickColor = BrickColor.new("Ghost grey")
PLight.Enabled = false
wait(0.2)
end
end
end
end
ClickSwitch.MouseClick:Connect(light)
maybe try something like this it uses a debounce and also spawn function to allow all the lights to be switched on or off at the same time then delays and changes your clicked variable which you are using as on/off
This could be setup better with the on/off but it should work could even use Attributes from server and render this on the clients…
local tweenservice = game:GetService("TweenService")
local SwitchInfo = TweenInfo.new()
local SwitchModel = script.Parent
local SwitchHinge = SwitchModel.PrimaryPart
local Switch = SwitchModel.Switchlever
local Lightbulb = SwitchModel.Parent.Parent.Lights
local Lights = SwitchModel.Parent.Parent:WaitForChild("Lights"):GetChildren();
local LightsOn = tweenservice:Create(SwitchHinge, SwitchInfo, {
CFrame = SwitchHinge.CFrame * CFrame.Angles(0, 0, math.rad(-300))
})
local LightsOff = tweenservice:Create(SwitchHinge, SwitchInfo, {
CFrame = SwitchHinge.CFrame * CFrame.Angles(0, 0, math.rad(0))
})
local ClickSwitch = Instance.new("ClickDetector")
local Clicked = false
ClickSwitch.Parent = Switch
ClickSwitch.Name = "ClickSwitch"
local Debounce -- use a debounce so they cant double run the code
local function light()
if Debounce then return end -- if already running then delay just pass your wait time in your script so .6
Debounce = true
task.delay(.6,function()
Debounce = nil
end)
for _, Light in pairs(Lights) do
task.spawn(function() -- add this allows all lights to turn on off at same time
if Light:FindFirstChild("LightPart") then
if Clicked then
local PLight = Light.LightPart.Light
LightsOff:Play()
wait(0.3)
SwitchModel["FlipSwitchOFF SFX"]:Play()
Light.LightPart.Material = Enum.Material.SmoothPlastic
Light.LightPart.BrickColor = BrickColor.new("Ghost grey")
PLight.Enabled = false
wait(0.2)
else
local PLight = Light.LightPart.Light
LightsOn:Play()
wait(0.3)
SwitchModel["FlipSwitchON SFX"]:Play()
Light.LightPart.Material = Enum.Material.Neon
Light.LightPart.BrickColor = BrickColor.new("White")
PLight.Enabled = true
wait(0.2)
end
end
end)
end
if not Clicked then -- change your clicked varaible after your lights have be turned on or off for the next function call
task.wait(.5)
Clicked = true
else
task.wait(.5)
Clicked = false
end
end
ClickSwitch.MouseClick:Connect(light)