Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a SpotLight turn on when a brick is a certain color?

Asked by 10 years ago

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.

5 answers

Log in to vote
0
Answered by 10 years ago

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
Ad
Log in to vote
0
Answered by 10 years ago
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


0
I tried putting this script into the spotlight but is still doesn't work in-game. ctrlboy 5 — 10y
0
Is there any output? Articulating 1335 — 10y
0
Not that I know of. ctrlboy 5 — 10y
0
Try this edit. Articulating 1335 — 10y
View all comments (2 more)
0
Still no result. am I maybe putting this in wrong? like do I have to change something in your edit? ctrlboy 5 — 10y
0
Are you sure your pathway to the variable Object is correct? Archonious2 160 — 10y
Log in to vote
0
Answered by
m0rgoth 75
10 years ago

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)
Log in to vote
-1
Answered by 10 years ago
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.

Log in to vote
-1
Answered by 10 years ago

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 use BrickColor.new("Colorname for example Bright red") just replace the Colorname with this. All while statement must have a wait() 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.

Answer this question