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 != 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
local npcs3 = npcs2 – Make another table with the distances
table.sort(npcs3) – Sort this (the lowest number will be first)
targetdist = npcs3 – 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.
hook.Add(“Think”,“aimbot”,aimbot) – The hook will spam “aimbot” until it finds a target…
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.