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)
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)
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.
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.