Organize & Assign Text, to Player Nametag

I want to know how to detect which player is closet to a part every second, then update an overhead name tag gui to say there position like

1st SpeedySanikJr

But when they are passed by robloxplaye6373, the passer will have the

1st robloxplayer6373

And now

2nd SpeedySanikJr

How do I do this?

This is what is meant by overhead name tag;
image

But I want the position number in front of the name, depending on how close they are to a part eg 1st, 2nd, 3rd

I know how to get the positions with this script:

local first = nil
local second = nil
local third = nil

local item = game.Workspace.apple


while true do
	for i,v in pairs(workspace:GetChildren()) do
	if v:FindFirstChild("HumanoidRootPart") then
		if (v.HumanoidRootPart.Position - item.Position).Magnitude < 10 and first == nil then
				first = (v.HumanoidRootPart.Position - item.Position)
		elseif (v.HumanoidRootPart.Position - item.Position).Magnitude < 10 and v ~= first then
				second = (v.HumanoidRootPart.Position - item.Position)
		elseif (v.HumanoidRootPart.Position - item.Position).Magnitude < 10 and v ~= second and v ~= first then
				third = (v.HumanoidRootPart.Position - item.Position)
			end
		end
	end
	wait(1)
end

But how do I organize and assign them to a players Build board Gui overhead name tag, based on there proximity to the apple?

Edit: should I create a whole new script, is this one effective at all? Is there a simpler way to do this?

1 Like

I believe what you meant is that you want to know how to order a selection of players from closest to furtherst to a specified point.

You will need:

• A array with the characters that will be ordered;
• The point from which the distance will be measured;
• A dictionary that matches the index (say, 5) with a string (“fifth”);
• A checking function, that will check the order and give the titles to the players;
• And a sorting function, that will order the characters by distance.

To sort the array, you can use table.sort!

• Also, the billboardguis should be inside the characters, to make matters easier. Offset them if you need!

Here is the checking function:

function Check()
    table.sort(char_array,sortbydistance)

    for Index,Character in ipairs(char_array) do
        local Billboard = Character["Whatever are the billboards adress go here"]

        Billboard.Text = title_dictionary[Index]
    end
end

After you fill in the blanks in it, it should do the following:
â–ş sort the array of characters
► for every character in this array, find it’s billboard and set its text property to the value of the key in the title_dictionary that matches the current Index.

you can find more about tables (arrays and dictionaries) here.

And here is the sorting function:

function sortbydistance(Char1, Char2)
    if (Char1.Position - ApplePosition).Magnitude > (Char2.Position - ApplePosition).Magnitude then
        return false
    else
        return true
    end
end

There is a spot in the DevHub that explains table.sort.

Simply, you give it an array, and a function. The function needs two parameters, which in this case is Char1 and Char2. It also needs to return either false or true. If it returns true, it means the first parameter - Char1, must come before; and if false, that it must come after.

So in this case, the function checks whether Char1’s distance to the apple is greater or lesser than Char2’s, and depending of the result, Char1 will be put before or after Char2 in the array.

I believe this all should work after you fill in the blanks, since i simplified this code some bits

1 Like

@Windsonnes
So what if I had 20 players, that I wanted it order 1st through 20th then what?

And do i put this in a server script, in the same script, or two different scripts?

Could you specify what I need to fill in please?

I’m a scripting noob, Sorry :neutral_face:

(Also for the second part, I will need to define the character before right?)

You should put the 20 player’s characters inside of the array, for them to be sorted/ordered;

I think you should do this in a separate ServerScript;

And, because you want to check a group of characters, you don’t really need to define a specific one. Just get them all inside of an array

To keep this array updated when players leave or join, you should use some simple PlayerAdded/PlayerRemoving events.

I believe you could also connect this code to a RunService.Heartbeat too.

@Windsonnes
Wait, I don’t understand. What do you mean by put 20 players characters inside of the array?

How do I get all the players characters, and connect it to the function?

Also I still don’t understand what blanks need to be filled in?..