Not a scripting issue but this drived me crazy for 5 mins straight and I need to know why

Hello scripters,

My question is not an issue with a script that I have instead it’s a question that I have from one of my working scripts which is this one:

local goldtool = script.Parent
local handle = script.Parent.Handle

local canChangeColor = true

handle.Touched:Connect(function(hit)
	if canChangeColor then
		print(hit)
		local hitPreviousBrickColor = hit.Color
		print(hitPreviousBrickColor)
		hit.BrickColor = BrickColor.new("Gold")
		wait(2)
		hit.BrickColor = BrickColor.new(hitPreviousBrickColor)
		canChangeColor = false
		print("you can't change hit colors for now")
		wait(5)
		canChangeColor = true
		print("you can change hit colors now")
	end
end)

So let me explain first what this is before you say anything, alright so I was doing an old challenge that I found on reddit just for fun I have successfully done it but my scripting mind just wouldn’t stop and go on to the next challenge instead I kept modifiying the script until I came across a problem which drived me crazy I couldn’t solve it for 5 mins straight trying to figure out what the heck is going on and why? and I am very good at solving scripting problems.

Eventually thought I fixed it but that still left me with so many questions that I would love to find answers to, so the questions are:

If you replace the hitPreviousBrickColor local variable to:

local hitPreviousBrickColor = hit.BrickColor

The script will error, watch this GIF to see for yourself:


And if you change the hitPreviousBrickColor local variable to:

local hitPreviousColor = hit.Color
--[[
And also change how you set the Part's previous Color
to hit.Color instead of hit.BrickColor
--]]

The script will not error, instead it will give me a weird Color3 value which is not the Color3 value that the Part had before it got it’s Color3 value changed, watch this GIF to see for yourself:


I am extremely curious as to why this is happening, and why it only worked when I took the Color3 value from the Part’s previous Color3 value and put it in BrickColor.new() and it also still has the weird Color3 value printed out.

1 Like

Changing this line to local hitPreviousBrickColor = hit.BrickColor might help?

1 Like

I have already tested it out with:

local hitPreviousBrickColor = hit.BrickColor

Read my post again for what happened.

I apologize for missing that out. Anyway, to answer the first part,

hit.BrickColor = BrickColor.new(hitPreviousBrickColor)

Correct me if I’m wrong, but I don’t think you can make a new BrickColor out of an already existing BrickColor using the BrickColor.new constructor. Therefore it might help if you tried to just assign the value directly.

hit.BrickColor = hitPreviousBrickColor

To answer the second part, I believe it’s because you tried to use a nonexistent variable.

“hitPreviousColor” and “hitPreviousBrickColor” are two separate variables. Therefore, the constructor will assume the Color3 value is 0, 0, 0 because that’s the default Color3 that will be made if no arguments are passed at all, which is the case because you’re trying to make a Color3 with values that don’t exist. That’s why your brick was assigned a black color.

2 Likes

Your answer to the first part is correct it works now when I try to set the hit.BrickColor to the hitPreviousBrickColor instead of putting it between the parenthesis of BrickColor.new().

However I couldn’t understand your answer to the second part I still don’t know where the weird Color3 value is coming from, could you explain it more? I don’t have two seperate variables I only have the hitpreviousColor variable and also it doesn’t print out that hit.Color = 0, 0, 0 instead it’s three 0’s with a lot of decimals after them you can see it being printed out in the first and second GIFs I also have no idea how it converts it into the previous Color3 value of the Part, if anyone knows where it’s coming from then I would love to hear an answer from them.

I was wondering if you changed this to hit.BrickColor = BrickColor.new(hitPreviousColor) since you wrote this line:

These are two separate variables. If you didn’t change it, it means that the constructor is being provided with values that don’t exist since the variable was never declared in the first place, which is what that gives you an arbitrary Color3 value.

1 Like

No, but back then I changed the line to be logical, for example if I had the hitpreviousBrickColor local variable I would change it to:

hit.BrickColor = BrickColor.new(hitPreviousBrickColor)

But now you said that this is wrong and I should change it to:

hit.BrickColor = hitpreviousBrickColor

And also back then if I had the hitpreviousColor local variable I would change it to:

hit.Color = Color3.new(hitPreviousBrickColor)

But this isn’t needed anymore because you fixed the issue

Alright, now I understand, thank you so much for helping me understand why and how all of this was happening!
Your a legend.

1 Like