So, I made a script to make an in-game blackout, where a person can go sabotage the generator and make the base in the dark, only I am not able to figure out why my script is not working. I would really appreciate help since I’ve looked everywhere and there’s nothing.
local button = script.Parent
local bool = script.Parent.Parent.Parent.Parent.Parent.Parent.Blackout
local Loading = script.Parent.Parent.Loading
button.MouseButton1Click:Connect(function()
if bool.Value == false then
bool.Value = true
Loading.BackgroundColor3 = Color3.new(255, 255, 0)
Loading.Text = "CARREGANDO..."
wait(3)
Loading.BackgroundColor3 = Color3.new(1, 0.333333, 0)
Loading.Text = "DESATIVO"
button.Text = "RESOLVER"
end
end)
local Lights = workspace.S1Lights.LightModel:WaitForChild("Light")
local Surface = workspace.S1Lights.LightModel
for i,v in pairs(Lights:GetChildren()) do
bool.Changed:Connect(function()
if bool.Value == true then
v.Enabled = false
end
end)
end
for i,f in pairs(Surface:GetChildren()) do
bool.Changed:Connect(function()
if bool.Value == true then
f.Material = Enum.Material.Metal
end
end)
end
The lights are all popped into a model to facilitate the script.
So, I made a new script but still didnt worked yet. No errors shown in the output.
local button = script.Parent
local bool = script.Parent.Parent.Parent.Parent.Parent.Parent.Blackout
local Loading = script.Parent.Parent.Loading
local S1 = game.Workspace.S1Lights
local Surface = S1.LightModel
button.MouseButton1Click:Connect(function()
if bool.Value == false then
bool.Value = true
Loading.BackgroundColor3 = Color3.new(255, 255, 0)
Loading.Text = "CARREGANDO..."
wait(3)
Loading.BackgroundColor3 = Color3.new(1, 0.333333, 0)
Loading.Text = "DESATIVO"
button.Text = "RESOLVER"
end
end)
for i,v in pairs(Surface:GetChildren("")) do
bool.Changed:Connect(function()
if bool.Value == true then
v.Material = Enum.Material.Metal
v.SurfaceLight.Enabled = false
end
end)
end
You’re only getting one of the light models with local Surface = S1.LightModel
To change all of them, use a for loop that goes through S1, and make sure all the models you are affecting are the light models by using if v.Name == "LightModel".
Also, you are giving :GetChildren() an argument, which it doesn’t take.
Then, you should be putting the for loop in the :Connect() function, rather than the :Connect() function in the for loop.
Lastly, the code in if bool.Value == true then is wrong, because it will affect all parts in the model, and also v is not filtered and therefore expects all the parts in the model to have a SurfaceLight instance.
This doesn’t matter since it would not work correctly with the new for loop I talked about in the first sentence. To fix this, just do v.Light
I managed to make a progress and now the lights go off, the problem is if I can’t use the if bool.Value == true then, how would I make the lights go off ONLY if a player in a team clicks the button to turn them off?
local lights = {}
for i,v in pairs(workspace.S1Lights:GetDescendants()) do
if v:IsA('PointLight') or v:IsA('SurfaceLight') or v:IsA('SpotLight') then
table.insert(lights,v)
end
end
for i,v in pairs(lights) do
v.Enabled = false
v.Parent.Material = Enum.Material.Metal
end
(The script in total)
local button = script.Parent
local bool = script.Parent.Parent.Parent.Parent.Parent.Parent.Blackout
local Loading = script.Parent.Parent.Loading
Activate = false
button.MouseButton1Click:Connect(function()
if bool.Value == false then
bool.Value = true
Loading.BackgroundColor3 = Color3.new(255, 255, 0)
Loading.Text = "CARREGANDO..."
wait(3)
Activate = true
Loading.BackgroundColor3 = Color3.new(1, 0.333333, 0)
Loading.Text = "DESATIVO"
button.Text = "RESOLVER"
end
end)
local lights = {}
for i,v in pairs(workspace.S1Lights:GetDescendants()) do
if v:IsA('PointLight') or v:IsA('SurfaceLight') or v:IsA('SpotLight') then
table.insert(lights,v)
end
end
for i,v in pairs(lights) do
v.Enabled = false
v.Parent.Material = Enum.Material.Metal
end