How do I store a player values in a table and get the players name and value

okay so im stuck at finding out how to fix this error. Im making a distance tracker and it takes the lowest number there and changes the text to that number and puts the name of player thats close to the local player.

I have read on Most efficiently best way to get the smallest numeric value in a table while also taking its index?
to find the lowest number but I do not know how to do table.insert for it.

and kept getting errors such as invalid argument #3 (string expected, got nil)

heres my script

local StudsText = Frame.Studs
local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
	character = player.CharacterAdded:wait()
end

local otherplayers = game.Players:GetChildren()

local playertable = {}
local totaltable = #playertable

function getLowest()
	local low = math.huge
	local index
	for i, v in pairs(playertable) do
		if v < low then
			low = v
			index = i
		end
	end
	return index
end

RunService.RenderStepped:Connect(function()
	local playerpos = character.Head.Position
	if #otherplayers > 0 then
		for i,players in pairs(otherplayers) do
			if players ~= player then
				local othercharacter = players.Character

				local otherplayerpos = players.Character.Head.Position
				local studsX = (playerpos.X - otherplayerpos.X)
				local studsZ = (playerpos.Z - otherplayerpos.Z)

				local studs = studsX + studsZ

				table.insert(playertable,totaltable+1,players.Name)
				playertable[players.name] = studs

				totaltable = totaltable + 1 
			end
		end
		StudsText.Text = getLowest()
	end
end)

I see 2 errors, and they are most likely the reason you are having trouble.

  1. the otherplayers variable should be inside of the RenderStepped so it updates
  2. you use player.name without the capital N after you insert the players name into the table.
    Edit: also, I have no idea why you are using the variable totaltable when you don’t use it, but either way, you should reset the table after you are done using it to just {} and totaltable to 0 so it doesnt keep adding up and eventually break.

Oh okay ill try to see if it works