"Comparing Two Userdata Values" Error

Hello Facepunch, I was using the basic aimbot tutorial and decided to make it so instead of aiming for the head when you go over an NPC, it should find the closest NPC to you and snap to its head. Here is my code:
[lua]function aimbot() – Starting the function
local ply = LocalPlayer() – Getting ourselves
if input.IsKeyDown(KEY_CAPSLOCK) then – If the capslock key is down
local npcs = ents.FindByClass(“npc_*”) – Make a table of all the NPCs
if npcs[1] != nil then – If the table is not nil (there are NPCs)
local npcs2 = npcs – Make a duplicate table of NPCs
for key, ent in ipairs(npcs2) do – Looping through that table
ent = ply:GetPos():Distance(ent:GetPos) – Change the values from the originals to the distance from that NPC to the player
end
local npcs3 = npcs2 – Make another table with the distances
table.sort(npcs3) – Sort this (the lowest number will be first)
targetdist = npcs3[1] – The first value (the lowest number (the shortest distance)) is our target’s distance
npcs2key = table.KeyFromValue(npcs2, targetdist) – Get the key of the first value in the second table (the one that is still in order, we can use this to find the original value)
target = npcs[npcs2key] – Our target is the original NPC that had its value changed to be the shortest distance (they have the same key, they were only rearranged in npcs3)
local targethead = target:LookupBone(“ValveBiped.Bip01_Head1”) – In this aimbot we only aim for the head.
local targetheadpos,targetheadang = target:GetBonePosition(targethead) – Get the position/angle of the head.
ply:SetEyeAngles((targetheadpos - ply:GetShootPos()):Angle()) – And finally, we snap our aim to the head of the target.
end
end
end
hook.Add(“Think”,“aimbot”,aimbot) – The hook will spam “aimbot” until it finds a target…
[/lua]
I get an error:


Hook 'aimbot' Failed: [lua\includes\extensions	able.lua:136] attempt to compare two userdata values

This means it is probably:
[lua]table.sort(npcs3) – Sort this (the lowest number will be first)[/lua]
Thanks in advance for help.
My method of messing around with tables to get the closest NPC may be confusing, but I just want to know why I can’t compare the distance of two objects from the player.

Change line 8 from
[lua] ent = ply:GetPos():Distance(ent:GetPos)[/lua]

To

[lua] ent = ply:GetPos():Distance(ent:GetPos())[/lua]

That’s the only error I saw, and it worked fine for me.

Edit: Realize that wasn’t the issue, sorry.

What line is line 136?

[editline]26th October 2011[/editline]

Nevermind.

Oh yeah, I converted it into something using Vector.Length(), and didn’t convert it back right for posting here. This is a clientside script that I run with lua_run_cl from the garrysmod/lua folder.

Also, Divinity, it only has this error when there are multiple NPCs on the map, have you tried that? Does anyone know what “comparing two userdata values” means?

[lua]
function CalculateTarget()
local distance = math.huge();
local target;

for k, v in pairs( ents.FindByClass("npc*") ) do
	if( v:GetMoveType() != 0 ) then
		if( (LocalPlayer():GetPos() - v:GetPos()):Length() < distance ) then
			distance 	= (LocalPlayer():GetPos() - v:GetPos()):Length();
			target 		= v;
		end
	end
end

return target;

end

function _R.Entity:HeadPos()
return self:LocalToWorld( self:OBBCenter );
end

hook.Add(“CreateMove”, “aimbot”, function( cmd )
local nTarg = CalculateTarget();
local hPos, angles = nTarg:HeadPos();
local angledif = LocalPlayer():GetShootPos();

angles = Angle(hPos - angledif);
angles.y = math.NormalizeAngle( angles.y );
angles.p = math.NormalizeAngle( angles.p );
angles.r = 0;

cmd:SetViewAngles( angles );

end)
[/lua]

Just for you.

Thanks! I don’t really understand that code though, would you mind commenting it?

[lua]
local distance = math.huge(); – declare a variable equal to infinity
local target; – declare a nil variable

function CalculateTarget() – start of function
for k, v in pairs( ents.FindByClass(“npc*”) ) do – loop through all the ents found on the map with the class of npc
if( v:GetMoveType() != 0 ) then – if its still alive
if( (LocalPlayer():GetPos() - v:GetPos()):Length() < distance ) then – if the length of the next target is less the last length found continue
distance = (LocalPlayer():GetPos() - v:GetPos()):Length(); – set distance to the length of the closest player
target = v; – set new found target
end – end if statement
end – end if statement
end – end for loop

return target; -- return the found closest target

end – end function

function _R.Entity:HeadPos() – open a function to calculate the headposition
return self:LocalToWorld( self:OBBCenter ); --returns center of entity (i didnt give you the code for a headshot, do that yourself
end – end function

hook.Add(“CreateMove”, “aimbot”, function( cmd ) – hook into CreateMove
local nTarg = CalculateTarget(); – set nTarg to the next found target
local hPos, angles = nTarg:HeadPos(); – set hPos to the position of the head of the target and angles to the angle of the found targets head
local angledif = LocalPlayer():GetShootPos(); – set angledif to the position of where you are looking

angles = Angle(hPos - angledif); -- set angles to the calculated angle between your shootpos and and the targets head
angles.y = math.NormalizeAngle( angles.y );
angles.p = math.NormalizeAngle( angles.p ); -- normalize angles so there are no out of range errors
angles.r = 0;
 
cmd:SetViewAngles( angles ); -- set your eye position to the calculated angle

end) – end hook
[/lua]

You’re a genius! I would’ve never thought of that way of doing it, although I’m no lua king.

I get:


[lua
ewbot.lua:18] function arguments expected near ')'

[editline]28th October 2011[/editline]

Also it’s math.huge not math.huge()

[editline]28th October 2011[/editline]

Okay, so I know the problem. I fixed your errors in the code and made it this:
[lua]
local distance = math.huge
local target
local ply = LocalPlayer()

function CalcTarget()
local plypos = ply:GetPos()
for k, v in ipairs(ents.FindByClass(“npc_*”)) do
local dist = v:GetPos():Distance(ply:GetPos())
if dist < distance then
target = v

		end
		
	end

return target

end

function Aimbot()
if input.IsKeyDown(KEY_CAPSLOCK) then
local mytarget = CalcTarget()
local targethead = mytarget:LookupBone(“ValveBiped.Bip01_Head1”) – In this aimbot we only aim for the head.
local targetheadpos,targetheadang = mytarget:GetBonePosition(targethead) – Get the position/angle of the head.
ply:SetEyeAngles((targetheadpos - ply:GetShootPos()):Angle()) – And finally, we snap our aim to the head of the target.

end

end
hook.Add(“Think”, “Aimbot”, Aimbot)
[/lua]
But now I get errors on line 23 saying that its using a nil value, after I finish killing everything. I think that it detects dead NPCs, but can’t get the positions of their bodies? How do I detect if it is nill and stop it?

  1. don’t use ipairs
    2.if the movetype of the entity is 0 dont aim at it