Would this GetClosestPlr() function work?

I made a new script that returns the closest player to you. I dont have many alt accs or online friends so can you guys tell me… Would this actually work?

local function ClosestPlayer(Point: Model): Model
   local Start_Position = Point.PrimaryPart.Position
   local Ending_Position = nil
   local Distance = nil
   local Distance_Table = {}
   local Name_Table = {}
   for i,player in pairs(game.Players:GetPlayers()) do
   	if player.Character.Name ~= Point.Name then
   		local Chosen_Character = player.Character
   		Ending_Position = Chosen_Character.PrimaryPart.Position
   		Distance = math.abs(Start_Position-Ending_Position.X)+
   			math.abs(Start_Position-Ending_Position.Y)+
   			math.abs(Start_Position-Ending_Position.Z)
   		table.insert(Distance_Table,Distance)
   		Name_Table["_"..Distance] = player.Name
   	end
   end
   local Smallest_Distance = math.min(table.unpack(Distance_Table))
   local Smallest_Distance_Player = Name_Table["_"..Smallest_Distance]
   print(Smallest_Distance_Player)
   return game.Players[Smallest_Distance_Player].Character
end

this is very complex for no reason, what are you suppose to compare distances with?
Because you can just do this

function Get(Part)
	for _,v in game:GetService("Players"):GetPlayers() do
		if v.Character and v.Character.PrimaryPart then
			if (v.Character.PrimaryPart.Position - Part.Position).Magnitude <= 15 then
				return true,v
			end
		end
	end
end
local IsNear:boolean?,ClosestPlayer:Player? = Get(HRP)
1 Like

Well, for this part, you arent doing the math correctly to get the Distance, the Pythagorean Theorem (sqrt(a^2 + b^2 = c^2) or sqrt(a^2 + b^2 + c^2 = d^2)) would help you get the Distance of an Object between p0 and p1, all you are doing is getting the Difference of p0's and p1's Axis, you are doing the first step correctly, but you need to square the number, add them together, and then find the Square Root, like so:

local p = (Start_Position - Ending_Position) -- Difference between p0 and p1
local Distance = math.sqrt(p.X^2 + p.Y^2 + p.Z^2) -- Distance of p0 and p1

However Vector3 Inherits a Method for this, Its called Magnitude

local Distance = (Start_Position - Ending_Position).Magnitude -- Distance of p0 and p1
1 Like

i Compare the distances at math.min(table.unpack(Distance_Table)) with eachother.

1 Like

I could test it on One player (my alt) and it worked and thx for the help its appreciated (fixed some parts)! :))

I know the topic has been solved but can you help me with this part in a script?

Effect.Parent = plr.Character.PrimaryPart
		local function ClosestPlayer(Point: Model): Model
			local Start_Position = Point.PrimaryPart.Position
			local Ending_Position = nil
			local Distance = nil
			local Distance_Table = {}
			local Name_Table = {}
			for i,player in pairs(game.Players:GetPlayers()) do
				if player.Character.Name ~= Point.Name then
					local Chosen_Character = player.Character
					Ending_Position = Chosen_Character.PrimaryPart.Position
					Distance = math.abs(Start_Position.X-Ending_Position.X)+
						math.abs(Start_Position.Y-Ending_Position.Y)+
						math.abs(Start_Position.Z-Ending_Position.Z)
					table.insert(Distance_Table,Distance)
					Name_Table["_"..Distance] = player.Name
				end
			end
			local Smallest_Distance = math.min(table.unpack(Distance_Table))
			local Smallest_Distance_Player = Name_Table["_"..Smallest_Distance]
			print(Smallest_Distance_Player)
			return game.Players[Smallest_Distance_Player].Character
		end
		local function Emit(Beam: Beam)
			Beam.Enabled = true
			delay(1.0,function()
				Beam.Enabled = false
			end)
		end
		local ClosePlr = ClosestPlayer(plr.Character)
		Effect.Up.Parent = ClosePlr.PrimaryPart
		Effect.Down.Parent = plr.Character.PrimaryPart
		Emit(script.Parent.Parent.PlaceHolder.Freeze.Line)
		wait(0.5)
		ClosePlr.PrimaryPart.Anchored = true
		script.Parent.Parent.PlaceHolder.Freeze.Up.Attachment.ParticleEmitter:Emit(1)
		script.Parent.Parent.PlaceHolder.Freeze.Up.ParticleEmitter:Emit(10)
		wait(5)
		ClosePlr.PrimaryPart.Anchored = false

For some reason it doesnt show any particles and Beams but i do emit them. (Also it anchors the closest character for 5 secs)

Its better to return the Player that is the closest, rather than who is within range, your code will return a person who is within the specified Distance, but not the person who is closest.

1 Like

can you help me find the problem?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.