Finding the closest player position without .magnitude

I am making a enemy that only runs after you when you are in a certain area. I will explain what I need help with in the notes in the script

local rig = script.Parent

local electricarea = script.Parent.Parent["Electric Area"]

local function getplayer()
		local region = Region3.new(electricarea.Position - (electricarea.Size/2), electricarea.Position + (electricarea.Size/2))
		local parts = workspace:FindPartsInRegion3(region,nil,100000)
		
		local playerspos = {}
		for i,v in pairs(parts) do
			if v.Parent:FindFirstChild("Humanoid") then
			table.insert(playerspos,v.Parent.HumanoidRootPart.Position)
			return playerspos
			end
	end
end
while wait(1) do
	local distances = {}
	for i ,v in pairs(getplayer()) do
		local distance = (rig.HumanoidRootPart.Position - Vector3.new(getplayer())).Magnitude
		table.insert(distances, distance)
	end
	local clostestdistance = table.unpack(distances)
	rig.Humanoid:MoveTo(Vector3.new(clostestdistance)) -- when i get .magnitude, it only gives me one position, so it will go far away, but i want to find the closest position, still having the position
	print(clostestdistance)
end

I probably explained this horrible, so ask about stuff ig

1 Like

I see posts like this all the time, dont use magnitude here, instead use this post;

ive already figured that part out using regions, the part in the script where i use magnitued is when i find the closest character to the boss, under the while loop

Ah, my apologies! I will read up on this.

Thankyou! Have you found a solution yet? I thought I could maybe add the magnitude onto the x and z axises but i didnt know which distance on the table was the one I should add it onto

.Magnitude isn’t doing anything wrong here, and I’m 100% sure there aren’t any better or less complicated methods to find the closest player that doesn’t use it. Your algorithm to search for the nearest player is just not done correctly.

This is how your algorithm should be

EDIT: Also, I don’t think your player searching algorithm works well either, it seems to only be getting one single player’s position so you don’t actually have anything to compare it to

Magnittude doesnt give you a vector 3 value. So you table is full of single values. So you are essentially sending the mob to, vector3(magnitude,0,0)

Also table.unpack doesnt sort the magnitude, you are doing a lot of things wrong. You want to compare the players magnitudes, to find the closest one. Then walk your mob to the player not to the magnitude but the players position

I read somewhere that table.unpack finds the smalest value in a table, and im trying to get the player, but i dont know how

the reason i have it like this, is becuase there are two sides of the area i built, and when the player crosses over to the other side, i want it to stop chasing the player

No unpack doesnt do that. It for when you want to split the table into its own variables https://developer.roblox.com/en-us/api-reference/lua-docs/table`

Oh thanks, I guess i miss read something lol

It’s still mostly wrong.

  • Your “getplayer()” function only returns a table with 1 position, so you can’t actually find the closest position if you don’t have any others to compare it to.
    Fix that by removing the ‘return’ and adding some extra checks to prevent the loop from finding the same player twice

  • Your algorithm to find the closest player in the list returned by getplayer() is also wrong.
    It’s not getting the closest distance as you aren’t actually comparing anything (check the post I tagged in my previous post for a good algorithm) and the MoveTo vector3 doesn’t actually go to wherever the player is because you haven’t stored the actual position anywhere, only the total distance.

i will fix the rest of it thanks! but I am trying to get the actual position, because i only know the magnitude, i dont know which player i got that magnitude from

1 Like

Once you fix the player algorithm stuff, you can store the closest position in a variable (not the magnitude, the position).

I’m not sure how to correctly guide you through it until you do fix it

Aight im gonna correct it in parts cause im at work rn
First off get plager should simple just get the players or get the once that are close

function getPlayerCharacters():
     local characters = {}
     for _, v in game:GetService("Players"):getPlayers()
         local char = v.Character
         If char then 
          table.insert(characters, v)
         end
end

I made this:

local rig = script.Parent

local electricarea = script.Parent.Parent["Electric Area"]

local region = Region3.new(electricarea.Position - (electricarea.Size/2), electricarea.Position + (electricarea.Size/2))

local function movetonearestplayer()
		local parts = workspace:FindPartsInRegion3(region,nil,100000)
		local nearestPlayer, nearestDistance
		for i,v in pairs(parts) do
		if v.Parent:FindFirstChild("Humanoid") and v.Parent:FindFirstChild("Health") then
			local character = v.Parent
			print(character)
			local player = game:GetService("Players"):GetPlayerFromCharacter(character)
			local distance = player:DistanceFromCharacter(rig:WaitForChild("HumanoidRootPart").Position)
			if not character or
				(nearestDistance and distance >= nearestDistance)
			then
				continue
			end
			nearestDistance = distance
			nearestPlayer = player
			end
	end
	local char = nearestPlayer.Character or nearestPlayer.CharacterAdded:Wait()
		rig:MoveTo(Vector3.new(char:WaitForChild("HumanoidRootPart").Position))

	end




local iflightning = 10
while wait(1) do
	movetonearestplayer()
end


he goes really far away for no reason, but with nearest player, i dont get when it found it, it just found the nearest distance and then set the nearest player valuable to the player in the for loop

i am making it so there are two areas, and the enemy cant go in the other area

It teleports him really far away, with the z and x axises at zero and the y axis it around 500

First off dont loop through every part in the workspace thats how you create lag