Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What is wrong with this colour changing script?

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

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.

1 comment

0

Thanks, come to think of it, that is exactly what my computer science teacher said when I made the same mistake on Python...

This discussion is preserved as it was. New answers, comments, and votes are closed.