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

What is wrong with this colour changing script?

Asked by
Mystdar 352 Moderation Voter
9 years ago

This is supposed to change the colour of a part depending on the mode, but doesn't.

local Colors = {BrickColor.new("Brown"), BrickColor.new("Dusty Rose"), BrickColor.new("Dark stone grey"), BrickColor.new("Med stone grey")}
local mode = script.Parent.Mode

function MakeSpark(pos)
    local effect = Instance.new("Part")
    effect.Name = "Effect"
    effect.Anchored = false
    effect.CanCollide = false
    effect.Shape = "Block"
    effect.formFactor = "Custom"
    effect.Material = Enum.Material.Plastic
    effect.Locked = true
    effect.TopSurface = 0
    effect.BottomSurface = 0
    effect.BrickColor = Colors[1]
    -- From here 
    if mode.Value == 1 or 2 then 
        effect.BrickColor = Colors[1]
    elseif mode.Value == 3 then
        effect.BrickColor = Colors[2]
    elseif mode.Value == 4 then 
        effect.BrickColor = Colors[3]

    end  
    -- To here, doesn't work
    effect.CFrame = CFrame.new(pos)
    effect.Parent = game.Workspace
end

Thanks.

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago
if mode.Value == 1 or 2 then

This is the problematic condition.


Lua's order of operations actually makes this equivalent to

if (mode.Value == 1) or 2 then

Which is to say, it will either be true or 2 (which will always pass the condition).

Explicitly changing the order of operations to compare to (1 or 2) won't work, because or is just a normal operator -- it produces one value. That is, (1 or 2) is just 1.


In order to compare something to be a or b, you have to be explicit:

if x == a or x == b then

In this case, you can just check

if mode.Value <= 2 then

so that you still avoid having the longer condition.

0
Thanks, come to think of it, that is exactly what my computer science teacher said when I made the same mistake on Python... Mystdar 352 — 9y
Ad

Answer this question