robloxapp-20240808-1740593.wmv (3.8 MB)
I have a light switch script and it works kinda but it doesn’t get all of the lights if you look at the video.
Script
local part = script.Parent.Parent.Parent.SwitchPart
local pos = part.Position
local ori = part.Orientation
local size = part.Size
local tweenService = game:GetService("TweenService")
local switchpart2 = script.Parent.Parent.Parent.SwitchPart2
local lights = false
local changes = {
Position = switchpart2.Position;
Orientation = switchpart2.Orientation
--Color = Color3.new(0, 0, 0)
--Size = size + Vector3.new(0, 0, 0)
}
local changes2 = {
Position = part.Position;
Orientation = part.Orientation
--Color = Color3.new(0, 0, 0)
--Size = size + Vector3.new(0, 0, 0)
}
local tweenInfo = TweenInfo.new(
1, -- Time taken for a full animation
Enum.EasingStyle.Sine, -- Animation Style
Enum.EasingDirection.InOut, -- Animation Type
0, -- Number of repeats (-1 is infinite)
false, -- Reverse?
1 -- Delay between animations
)
local tween = tweenService:Create(part, tweenInfo, changes)
local tween2 = tweenService:Create(part, tweenInfo, changes2)
local lvlights = game.Workspace.LivingRoom.LivingRoomLights
script.Parent.Triggered:Connect(function()
for _, v in pairs(lvlights:GetDescendants()) do
if v:IsA("SpotLight") then
if not lights then
lights = true
tween:Play()
v.Enabled = true
v.Parent.Material = Enum.Material.Neon
v.Parent.BrickColor = BrickColor.new(237, 234, 234)
else
lights = false
tween2:Play()
v.Enabled = false
v.Parent.Material = Enum.Material.Plastic
v.Parent.BrickColor = BrickColor.new(0, 0, 0)
end
end
end
end)
I’m guessing this is server script? I say you should yield the script until the game fully loads like task.wait(1) because it cannot load in certain objects in time. Let me know if that works.
script.Parent.Triggered:Connect(function()
if not lights then
for _, v in pairs(lvlights:GetDescendants()) do
if v:IsA("SpotLight") then
lights = true
tween:Play()
v.Enabled = true
v.Parent.Material = Enum.Material.Neon
v.Parent.BrickColor = BrickColor.new(237, 234, 234)
rs.Values.LightsTurnedOff.Value -= 1
end
end
else
for _, v in pairs(lvlights:GetDescendants()) do
if v:IsA("SpotLight") then
lights = false
tween2:Play()
v.Enabled = false
v.Parent.Material = Enum.Material.Plastic
v.Parent.BrickColor = BrickColor.new(0, 0, 0)
rs.Values.LightsTurnedOff.Value += 1
end
end
end
end)
The general implementation, while functional, is inefficient.
-- Spotlight cache
local spotlights = {}
for _, v in pairs(lvlights:GetDescendants()) do
if v:IsA("SpotLight") then
table.insert(spotlights, v)
end
end
local function updateLightsState(state)
local material = state and Enum.Material.Neon or Enum.Material.Plastic
local color = state and BrickColor.new(237, 234, 234) or BrickColor.new(0, 0, 0)
local stateval = enable and -1 or 1
if state then
tween:Play()
else
tween2:Play()
end
for _, v in pairs(spotlights) do
v.Parent.Material = material
v.Parent.BrickColor = color
v.Enable = state
end
rs.Values.LightsTurnedOff.Value = rs.Values.LightsTurnedOff.Value + stateval
end
script.Parent.Triggered:Connect(function()
lights = not lights
updateLightsState(lights)
end)