Player distance script breaks after amount of time

I have a script that detects the distance between the nearest player, but on regular occasions the script just breaks after a certain amount of time. I think it breaks because someone left or joined, and their humanoid didn’t exist when it was searching, but my solutions didn’t work.

local PlayerDistances = { }
local LocalPlayer = game.Players.LocalPlayer
local players = game.Players.NumPlayers

while wait(1) do
	if players >= 2 then  -- Make check for that amount of characters actually existing or something idk, this breaks after a certain amount of time, I belive people leaving and joining.
		PlayerDistances = { }
		for _, player in pairs(game.Players:GetPlayers()) do
			if player ~= LocalPlayer then
				local Character = player:WaitForChild("Character")
				local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
				local LocalPlayerCharacter = LocalPlayer.Character
				local LocalPlayerHumRootPart = LocalPlayerCharacter:FindFirstChild("HumanoidRootPart")

				local Magnitude = (HumanoidRootPart.Position - LocalPlayerHumRootPart.Position ).Magnitude
				table.insert(PlayerDistances, Magnitude)
			end
		end
	script.Parent.PLA_Traits.PlayerDistances.Value = math.min(unpack(PlayerDistances))
	else
		script.Parent.PLA_Traits.PlayerDistances.Value = 99999999999
	end
end
2 Likes

If the problem is the Humanoid, you can bypass this issue by doing the following;

local Players = game:GetService("Players")

local PlayerDistances = {}

while task.wait(1) do --//Use the task library, it's bad practice not to
	for i, player in Players:GetPlayers() do --//Using (i)pairs is redundant
		if not player:FindFirstChild("Humanoid") then continue end
		...

This will skip that player in the for .. do loop to avoid it trying to state the Humanoid. If this does not fix the issue, please follow up with a screenshot of your error log and which line of code fails.

2 Likes

here is the error: 18:35:57.788 Players.Ender_devTC.PlayerGui.PLA.PlayerDistancesScript:19: missing argument #1 to ‘min’ (number expected) - Client - PlayerDistancesScript:19
18:35:57.788 Stack Begin - Studio
18:35:57.788 Script ‘Players.Ender_devTC.PlayerGui.PLA.PlayerDistancesScript’, Line 19 - Studio - PlayerDistancesScript:19
18:35:57.788 Stack End - Studio

local Players = game:GetService("Players")
local LocalPlayer = game.Players.LocalPlayer
local PlayerDistances = {}

while task.wait(1) do --//Use the task library, it's bad practice not to
	for i, player in Players:GetPlayers() do --//Using (i)pairs is redundant
		if player ~= LocalPlayer then
			PlayerDistances = {}
			if not player:FindFirstChild("Humanoid") then continue end
			
			local Character = player:WaitForChild("Character")
			local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			local LocalPlayerCharacter = LocalPlayer.Character
			local LocalPlayerHumRootPart = LocalPlayerCharacter:FindFirstChild("HumanoidRootPart")
			local Magnitude = (HumanoidRootPart.Position - LocalPlayerHumRootPart.Position ).Magnitude
			table.insert(PlayerDistances, Magnitude)
		end
	end
	script.Parent.PLA_Traits.PlayerDistances.Value = math.min(unpack(PlayerDistances))
	print(script.Parent.PLA_Traits.PlayerDistances.Value)
end

I think the error is that when theres no humanoid it never enters a value into the table and it breaks.

I remade the script into what I think is the result that you need. This will accurately print whoever’s player you are closest to.


Script located in StarterPlayer → StarterPlayerScripts → LocalScript

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

while task.wait(1) do --//Use the task library, it's bad practice not to
	local allPlayers = Players:GetPlayers() --//Make a list of all Players in the server. This will refresh every repeat
	local distanceOtherPlayers = {}
	local closestPlayer = nil
	local smallestDistance = math.huge
	table.remove(allPlayers, (table.find(allPlayers, player))) --//Remove the LocalPlayer from the list by using table.remove, using table.find to get the right index in the list
	
	for _, v in allPlayers do
		if not v.Character:FindFirstChild("HumanoidRootPart") then print("Character not yet loaded.") continue end --//Make sure it can't error
		
		local distanceToPlayer = (v.Character.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude --//Get the distance of that that player from the local player
		
		table.insert(distanceOtherPlayers, distanceToPlayer) --//Insert it into a separate table
		
		if distanceToPlayer < smallestDistance then --//Compare it to all the others to get the smallest distance
			closestPlayer = v --//Used for calling the player later if necessary
			smallestDistance = math.round(distanceToPlayer * 10) / 10 --//Round the distance to once decimal for readability
		end
		
	end
	
	print(smallestDistance, "studs away from", closestPlayer) --//Debug
	
end

I am aware that it could not be the most efficient, but it works.

2 Likes

Thanks this worked perfectly, thanks so much for your help!

1 Like

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