Attempt to call a nil value

Hi! I am making a custom collision system for a small game of mine, but I get the “Attempt to call a nil value” error. The error is on line 102. I know what the error means but I have no idea why I get the error, since I tested the code in “inCommand” and it worked perfectly. Could anyone explain me why I get the error or help me fix it?
Thanks in advance.

while true and wait(0.1) do
	local collisions = computeCollision(player)
	for i, v in pairs(collisions) do
		local funcs = {
			[-1] = "max",
			[1] = "min"
		}

		player.Position = Vector3.new(
			math[funcs[v.Position.X/math.abs(v.Position.X)]](player.Position.X, v.Position.X), -- line 102
			player.Position.Y,
			math[funcs[v.Position.Z/math.abs(v.Position.Z)]](player.Position.Z, v.Position.Z)
			)
	end
end

I think I know where is it. What’s ComputeCollision?

It’s a function that I created:

function computeCollision(part)

return game.Workspace:FindPartsInRegion3(Region3.new(Vector3.new(part.Position.X-part.Size.X/2, part.Position.Y-part.Size.Y/2, part.Position.Z-part.Size.Z/2),Vector3.new(part.Position.X+part.Size.X/2, part.Position.Y+part.Size.Y/2, part.Position.Z+part.Size.Z/2)),nil,5)

end

player is only Game.Players.LocalPlayer?

No, player is a brick character or something. Its just one part.

Where is line 102? We can’t see the line.

What are you passing as player in the computeCollisions function?

oops, edited the script. the line is now commented

1 Like

A part that is the player’s custom character. The function searchs for any other parts colliding with the character part and returns them

It can be the “math” word? Idk

I need to go I gonna answer later.

No it can’t, I think. The only thing that could be wrong is the “funcs[v.Position.X/math.abs(v.Position.X)]]” part, if it somehow isn’t equal to -1 or 1.

Ok I think I found the error. Its what I said: If the part’s position is 0,whatever,0 the equation is going to return nil, since there is only “-1” and “1” in the table. The didn’t dissapear so I’ll just use roblox physics until I find out what caused it. Thanks for the help.

Oh that’s true, it works? That makes sense

And another thing, why math with nothing behind can works. I didn’t know that thing

It works because I used a table like this:

local table = {
    [-1] = "min",
    [1] = "max"
}

math.min()

is the same like

math[“min”]

Wait, math.min() is actually a function?

returns smallest number in a tuple of numbers.

try something like this instead of having a custom table of math functions.

local min, max = math.min, math.max

print(min(5,21),max(2,534)) -- example

Your code is mostly good, except for this bit:

The problem may not be initially obvious, but you are indexing something that doesn’t exist.

To fix that, you would change what I quoted to this:

local funcs = {
	[-1] = "max",
	[0] = function() end,
	[1] = "min"
}
local xpos = math.clamp(v.Position.X, -1, 1)
xpos = xpos == 0 and 0 or (xpos < 0 and -1 or 1)
local zpos = math.clamp(v.Position.Z, -1, 1)
zpos = zpos == 0 and 0 or (zpos < 0 and -1 or 1)

player.HumanoidRootPart.CFrame = CFrame.new(
                        math[funcs[xpos]](player.Position.X, v.Position.X),
			player.Position.Y,
			math[funcs[zpos]](player.Position.Z, v.Position.Z)
)