Spawn BrickColor not working?

I have a script to change the color of a BasePart for 2 seconds, but it won’t work.

script.Parent.Touched:Connect(function()
	print("Touched.")
	script.Parent.BrickColor = "Insitutional white" 
	wait(2)
	script.Parent.BrickColor = "Medium stone grey"
end)

The error says BrickColor was expected but it got a string, which is weird because BrickColor is a string?

1 Like

BrickColor is a BrickColor Object.

Try this:

script.Parent.Touched:Connect(function()
	print("Touched.")
	script.Parent.BrickColor = BrickColor.new("Insitutional white") 
	wait(2)
	script.Parent.BrickColor = BrickColor.new("Medium stone grey")
end)
1 Like

It still doesn’t work, it prints touched though.


also btw this is a Script inside of every checkpoint (Server script)

There’s no debounce so your part’s pseudo-infinitely turning back to its first colour. Your code, otherwise, works fine - it’s just an oversight that you haven’t accounted for.

local debounce = false

Touched:Connect(function ()
    if not debounce then return end
    debounce = true
    -- Colour your parts
    debounce = false
end)
1 Like

Are you checking to make sure the part isn’t already being touched?

local IsBeingTouched = false
script.Parent.Touched:Connect(function()
	if not IsBeingTouched then
		IsBeingTouched = true
		print("Touched.")
		script.Parent.BrickColor = BrickColor.new("Institutional white")
		task.wait(2)
		script.Parent.BrickColor = BrickColor.new("Medium stone grey")
		IsBeingTouched = false
	end
end)

I have verified that this code works as intended.

Try using Color3.fromRGB(R,G,B)

script.Parent.Touched:Connect(function()
	print("Touched.")
	script.Parent.BrickColor = Color3.fromRGB(255,255,255) 
	wait(2)
	script.Parent.BrickColor = Color3.fromRGB(0,0,0)
end)
1 Like

4 flaws in the original code.

  1. A single wait. You will only see institutional white (only if this is short code within a loop)
  2. Misspelled Institutional white with Insitutional white
  3. You need to wrap it in BrickColor.new()
  4. Needs a debounce because the Touched happens multiple times very fast.
    Full code:
script.Parent.Touched:Connect(function()
	print("Touched.")
    script.Parent.CanTouch = false
	script.Parent.BrickColor = BrickColor.new("Insitutional white")
	wait(2)
	script.Parent.BrickColor = BrickColor.new("Medium stone grey")
    wait(2)
    script.Parent.CanTouch = true
end)
2 Likes

Thank you both, I’m going to use a combination of the debounce script, for debounce, and the Color3.fromRGB for the color.

1 Like

My script doesn’t work, I get a stack message.

script is:

local IsBeingTouched = false
script.Parent.Touched:Connect(function()
	if not IsBeingTouched then
		IsBeingTouched = true
		print("Touched.")
		script.Parent.BrickColor = Color3.fromRGB(0, 170, 0)
		task.wait(2)
		script.Parent.BrickColor = Color3.fromRGB(163,162,165)
		IsBeingTouched = false
	end
end)

The problem with that script at the moment is that “BrickColor” takes a BrickColor object, and Color actually uses Color3. You should change the script.Parent.BrickColor to just script.Parent.Color.

And another thing - the .Touched() event fires when anything touches the part, that includes other parts, not just players!

Alright, so when I do that, will the rest of the code work? Also, do you know a better solution to if touched?

Utilizing .Touched is a perfectly acceptable way of handling checkpoints. But you need to check what is exactly touching the part.

I recommend looking through the Touched Event documentation on the Wiki, and checking out maybe some YouTube tutorials on the event if that fits you better.

I used the code below, and this works perfectly, thank you all for the help! The problem was that 1, Color is easier to use, and I for color you have to you HSV, which is a 0-1 scale :smiley:

local IsBeingTouched = false
script.Parent.Touched:Connect(function()
	if not IsBeingTouched then
		IsBeingTouched = true
		print("Touched.")
		script.Parent.Color = Color3.fromHSV(0.457833, 1, 0.666667)
		task.wait(2)
		script.Parent.Color = Color3.fromHSV(0.722222, 0.0181888, 0.647059)
		IsBeingTouched = false
	end
end)