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

how do i change part color using if statements?

im trying to change the part color everytime you click it. it goes in order of pink, blue, red, green, and then back to pink. i dont know why this wont work? Nothing is in the output.

local click = script.Parent.ClickDetector
partcolor = script.Parent.BrickColor
local sound = script.Parent.Beep

click.MouseClick:connect(function()
	if partcolor == "255, 0, 191" then
		sound:Play()
		partcolor = "0, 0, 255"
	end

	if partcolor == "0, 0, 255" then
		sound:Play()
		partcolor = "255, 0, 0" 
	end

	if partcolor == "255, 0, 0" then
		sound:Play()
		partcolor = "0, 255, 0"
	end

	if partcolor == "0, 255, 0" then
		sound:Play()
		partcolor = "255, 0, 191"			
	end
end)

please help, thank you

1 answer

.BrickColor can't be assigned to a string value, instead you can do a BrickColor.new() value so when you set partcolor, for example on line 8 it could be partcolor = BrickColor.new(0,0,255). The final code would be:

local click = script.Parent.ClickDetector
partcolor = script.Parent.BrickColor
local sound = script.Parent.Beep

click.MouseClick:connect(function()
    if partcolor == BrickColor.new(255, 0, 191) then
        sound:Play()
        partcolor = BrickColor.new(0,0,255)
    end

    if partcolor == BrickColor.new(0, 0, 255) then
        sound:Play()
        partcolor = BrickColor.new(255, 0, 0)
    end

    if partcolor == BrickColor.new(255, 0, 0) then
        sound:Play()
        partcolor = BrickColor.new(0, 255, 0)
    end

    if partcolor == BrickColor.new(0, 255, 0) then
        sound:Play()
        partcolor = BrickColor.new(255, 0, 191)           
    end
end)

I hope this helped! If this did solve your problem might you select this as the answer, Thanks!

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