How to use part sizes in if statements?

I’m new to scripting and can’t seem to find anywhere what’s wrong with this if statement (below)
I don’t understand why it’s not letting me use vector3, it gives me the error: Workspace.Scripts.Clicked.Script:9: attempt to compare Vector3 <= Vector3
What should I be using, or what am I doing wrong? Any help is appreciated!

local detect = script.Parent.ClickDetector
local part = script.Parent
local originalsize = part.Size
local debounce = true

detect.MouseClick:Connect(function(player)
	if debounce then
		debounce = false
		if part.Size <= Vector3.new(20, 20, 20) then
			part.Size = part.Size + originalsize
			part.BrickColor = BrickColor.Random()
		elseif part.Size >= Vector3.new(20, 20, 20) then
			part.Size = part.Size - originalsize
			part.BrickColor = BrickColor.Random()
		end	
		wait(0.5)
		debounce = true
	end
end)

You can’t really check this with Vector3, however you could check the X/Y/Z size.
Example:

if Part.Size.X > 20 then

You can use this if the part’s XYZ will be the same size.

1 Like

The problem you are facing is that the >= and <= symbols are trying to compare a number with a Vector3 value which will error because those symbols are generally used to compare numbers.

One way to fix this issue is to check the the Vector3 XYZ properties individually with the size you want to compare it with:

local detect = script.Parent.ClickDetector
local part = script.Parent
local originalsize = part.Size
local debounce = true

detect.MouseClick:Connect(function(player)
	if debounce then
		debounce = false
		if part.Size.X <= 20 and part.Size.Y <= 20 and part.Size.Z <= 20 then
			part.Size = part.Size + originalsize
			part.BrickColor = BrickColor.Random()
		elseif part.Size.X >= 20 and part.Size.Y >= 20 and part.Size.Z >= 20 then
			part.Size = part.Size - originalsize
			part.BrickColor = BrickColor.Random()
		end	
		wait(0.5)
		debounce = true
	end
end)
1 Like

Ok thanks! That works just fine, but is there any way to check if all x,y,z are bigger/smaller, or do I have to always check individually?

It seems like you need to check the XYZ if you want precise results (you could use magnitude as well), but that’s only helpful if you compare it with another part’s position, etc.

But a vector of 0, 0, 0 always has a magnitude of 0, so you can detect if the magnitude is >= 0 (when using magnitude), if anything.

1 Like

Ah I see, thanks for the elaboration! That helps a lot. (:

1 Like

You’re welcome! I also edited my post for more information.

1 Like

You can’t compare Vector3’s using <=, <, >, or >= because Roblox doesn’t implement __lt nor __le metamethods.

That’s the real reason behind this, since you only mentioned error.

1 Like

As the other posts have suggested the comparison operators do not work when used on Vector3 values.
Instead of using something like:

part.Position.X >= 12
part.Position.Y <= 5
part.Position.Z > = 44

To compare all 3 components of the Vector3 value with some other value you could use:

local part = workspace.Part

for i, v in pairs({["X"] = 1, ["Y"] = 1, ["Z"] = 1}) do
	if part.Position[i] <= v then
		print("Hello!")
	end
end

This will work for any part, all you need to do is replace the part reference (if the part has a different name), the number “1” if you want to compare against a different coordinate & the comparison operator, if you’d rather check for greater than etc.

1 Like

Cool! Good to know, thank you.