Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I'm making an aimbot, how do you find the nearest humanoid using magnitude?

How do you find the nearest humanoid? Here's what I have, sorry if my code is trash I'm new to lua

local m = game.Players.LocalPlayer:GetMouse()
db = true
m.KeyDown:connect(function(k)
k = k:lower()
if k == "e" then
if db == true then
target = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
print("[Aimbot]: Now aimbotting: "..target.Name)
target2 = target.Character.Torso
local b = Instance.new("RocketPropulsion", game.Players.LocalPlayer.Character.Torso)
b.Target = target2
b.TurnP = 5000
b.MaxThrust = 0
b.MaxSpeed = 0
b.ThrustP = 0
b.CartoonFactor = 1
b:Fire()
end
end
end)

All I do is press "E" to switch my aim between players but it sometimes aimbots myself and glitches out also how do I find the nearest humanoid so I can change it and make it auto aim to the nearest player?

It only aimbots when you're not using lock-shift or 1st person but it's ok.

I know there's a way how to find the nearest player using magnitude but how do I do that?

https://scriptinghelpers.org/questions/54060/how-do-i-use-magnitude-to-find-the-nearest-player-in-the-game

Also I do not understand a single thing in the link above because I'm new to Lua, please help me :(

1 answer

✓ Accepted answer

Quick Explanation on Magnitude

Any mathematical vector has a magnitude and a direction. The magnitude is simply the size of a vector. Here's an article on vectors if you're unfamiliar with them.

Sample function

local function GetNearestHumanoid(targetPosition)
	local distance, humanoid = math.huge

	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		local character = player.Character
		if character then
			local characterDistance = (targetPosition - character.PrimaryPart.Position).magnitude
			if characterDistance < distance then
				distance = characterDistance
				humanoid = character.Humanoid
			end
		end
	end

	return humanoid
end

In the above function, I subtract the vectors by each other to get a single vector and get it's magnitude property. This essentially gets the distance between the tip of both vectors.

3 comments

1

You should probably mention that the function expects a Vector3 argument, or change the parameter to 'targetPosition' or something similar. Otherwise, good post.

0

Wow! Thanks.

0

Lol even though this is 3y old i've configured it and it worked, thanks so much man. Nothing else worked and ive been trying for a day

This discussion is preserved as it was. New answers, comments, and votes are closed.