ERROR: attempt to perform arithmetic (add) on Vector3 and number, how to solve the problem?

Who can help solve the problem with the engine, I do not know what the problem is, added vector3 no result, still writes this error

b_size = 3

function PtoT(x,y,z)
if type(x) == “userdata” then
x,y,z = x.X,x.Y,x.Z
end
return Vector3.new(math.floor(x/b_size + .5)),Vector3.new(math.floor(y/b_size + .5)),Vector3.new(math.floor(z/b_size + .5))
end

3 Likes

Is b size a vector value if so you cant add a regular number to them it has to vector + vector. for example
this would work

local vector1 = Vector3.new(0,2,0)

local vector2 = Vector3.new(0,3,0)

print(vector1 + vector2)

this would not work

local vector1 = Vector3.new(0,2,0)

print(vector1 + 5)
1 Like

I could not solve the problem, can you give me your dc?

1 Like

Lets just debug it here shall we? that way other people can input there answers to.

2 Likes

It’s because you’re trying to add a number to a Vector3. It isn’t possible because a Vector3 data type has 3 properties/numbers, X, Y and Z respectively while a number only has one.

If you want to specify which property to add to (X, Y or Z), you need to create a new Vector3 with the number you wish to add in it. So for example if you wanted to add to the X.

-- Vector3(X, Y, Z)

local vector1 = Vector3.new(0,2,0)
print(vector1 + Vector3.new(5,0,0))

-- result: 5,2,0
1 Like

Okay, here’s the problem

b_size = 3

function PtoT(x,y,z)
	if type(x) == "userdata" then
		x,y,z = x.X,x.Y,x.Z
	end
	return math.floor(x/b_size + .5),math.floor(y/b_size + .5),math.floor(z/b_size + .5)
end
1 Like

Adding or subtracting vectors with a number is not possible. You need to add a another vector3 to it like this:

local v = Vector3.new(1,7,3) + Vector3.new(3,2,4) -- X:4, Y:9, Z:7

The only kind of single number math that can work on vector3 or cframe is multiplication and division

local v = Vector3.new(15,20,5) / 2 -- X:7.5, Y:10, Z:2.5
local v2 = Vector3.new(15,20,5) * 2 -- X:30, Y:40, Z:10
1 Like
function PtoT(x,y,z)
	if type(x) == "userdata" then
		x,y,z = x.X,x.Y,x.Z
	end
	return math.floor(x/b_size + .5),math.floor(y/b_size + .5),math.floor(z/b_size + .5)
end

What needs to be changed here?

Does it still give you the same error when you run it?

1 Like

I did not understand what you wrote, can you correct this error, here is the code

b_size = 3

function PtoT(x,y,z)
	if type(x) == "userdata" then
		x,y,z = x.X,x.Y,x.Z
	end
	return math.floor(x/b_size + .5),math.floor(y/b_size + .5),math.floor(z/b_size + .5)
end

As @BappoSama Said, you cannot add Vector3 Value with a regular number, you need to make a new Vector3 Value then add it.

Maybe there are 2 variables named X, Y and Z

function PtoT(x,y,z)

X,Y,Z = x.X,x.Y,x.Z

when i tested it, it doesnt give that error, instead it just says 0, 0, 0 0, 0, 0 0, 0, 0,

i dont understand a single line here

Looks to be a type() problem, I guess? I changed type(x) == "userdata" to typeof(x) == "Vector3", and made 3 local variables that are the same as x, y, and z.

b_size = 3

function PtoT(x,y,z)
	local X, Y, Z = x, y, z
	if typeof(x) == "Vector3" then
		X,Y,Z = x.X,x.Y,x.Z
	end
	return Vector3.new(math.floor(X/b_size + .5), 0, 0),
		Vector3.new(0, math.floor(Y/b_size + .5), 0),
		Vector3.new(0, 0, math.floor(Z/b_size + .5))
end