I need help on scripting a Spotlight object so when its parent object's BrickColor is bright green, the light will be enabled and when the BrickColor is bright red, the light will be disabled.
I've tried scripting this myself but failed because I'm new to scripting with lua.
Here is what I tried so far:
while true do if Game.Workspace.CartRegen.Cart.On.BrickColor = "Bright red" then Game.Workspace.CartRegen.Cart.On.SpotLight.Enabled = false else if Game.Workspace.CartRegen.Cart.On.BrickColor = "Bright green" then Game.Workspace.CartRegen.Cart.On.SpotLight.Enabled = true end end
Please help me fix this script.
You have to put BrickColor.new("Colorname"). Also 'if' statement conditions have to have a == sign, which means it is equivalent to, one equals sign means to appoint something.
if Game.Workspace.CartRegen.Cart.On.BrickColor == BrickColor.new("Bright red") then
local Object = Game.Workspace.CartRegen.Cart.On function updateLight(Object) Object.SpotLight.Enabled = Object.BrickColor == BrickColor.new("Bright red") and false or true --If Object is red, then disable the light. Otherwise, enable it. end while wait(0) do updateLight(Object) end
These comments above would certainly answer your problem, however relying on infinite loops to solve your problems can cause lag in the future. To avoid such loops, we require events to fire whenever a specific thing happens. In this case, we want to see when that brick changes colour and to then turn on or off the light. If you look on the wiki, however, you will find that there is no .ColorChanged() event. But, there is a .Changed() event. This is how to use it:
local part = Game.Workspace.CartRegen.Cart.On part.Changed:connect(function(property) --The argument passed to the event is the name of the property that changed. if property == "BrickColor" then --Make sure the property changed is the colour. if part.BrickColor.Name == "Bright red" then --The name property or BrickColors makes this check easier. part.SpotLight.Enabled = false elseif part.BrickColor.Name == "Bright green" then --You could probably just replace this line with 'else' but I don't know if you intend on changing the part to colours besides red and green. part.SpotLight.Enabled = true end end end)
while true do if Game.Workspace.CartRegen.Cart.On.BrickColor == "Bright red" then Game.Workspace.CartRegen.Cart.On.SpotLight.Enabled = false elseif Game.Workspace.CartRegen.Cart.On.BrickColor == "Bright green" then Game.Workspace.CartRegen.Cart.On.SpotLight.Enabled = true end end
With given information, all you need to do is that. Should work, at least.
The problem with this script is that the if,BrickColor, and while statement is Wrong
Here is the fixed script
while true do if Game.Workspace.CartRegen.Cart.On.BrickColor = BrickColor.new("Bright red") then Game.Workspace.CartRegen.Cart.On.SpotLight.Enabled = false else if Game.Workspace.CartRegen.Cart.On.BrickColor = BrickColor.new("Bright green") then Game.Workspace.CartRegen.Cart.On.SpotLight.Enabled = true end wait(How long time it have to wait) end
When you use the
if
statement, then it uses the comparison operator which is this==
. When you want to Compare or assign a BrickColor, then you need to useBrickColor.new("Colorname for example Bright red")
just replace the Colorname with this. Allwhile
statement must have await()
function (which makes it wait until it exceeds the end then it continues) or the game will crash because your computer will use so much CPU that you'll not be able to play it.