Why can't I use this for a String Value?

So I have this piece of code that finds the player with the most of a data but whenever I try and export the winning player to a String Value with their username it comes back with a errors as nil and doesn’t export it.
Here is the code (Someone on Devfourm helped right it):
wait(5)

local function FindTopPlayers()
	local One = -1
	local Two = -1
	local Three = -1
	local Plr1, Plr2, Plr3

	for _, plr in ipairs(game.Players:GetPlayers()) do

		if One < plr.playtime.PlaytimeValue.Value then
			Plr3 = Plr2
			Three = Two

			Plr2 = Plr1
			Two = One

			Plr1 = plr
			One = plr.playtime.PlaytimeValue.Value
		elseif Two < plr.playtime.PlaytimeValue.Value then
			Plr3 = Plr2
			Three = Two

			Plr2 = plr
			Two = plr.playtime.PlaytimeValue.Value
		elseif Three < plr.playtime.PlaytimeValue.Value then
			Plr3 = plr
			Three = plr.playtime.PlaytimeValue.Value
		end
	end
	game.Players:FindFirstChild(Plr1).Team = game.Teams.Mayor
end

FindTopPlayers()
1 Like

Honestly you could just store all the values inside of a table and use table.sort to find out who is rank 1, 2 and 3

I used print(plr1) and it came back correct, but when I try to get that name out into a String Value or find the player in game.Players it comes back with this error:

ServerScriptService.MayorTesting:31: attempt to index nil with 'Team'  -  Server - MayorTesting:31
  16:27:15.193  Stack Begin  -  Studio
  16:27:15.193  Script 'ServerScriptService.MayorTesting', Line 31 - function FindTopPlayers  -  Studio - MayorTesting:31
  16:27:15.194  Script 'ServerScriptService.MayorTesting', Line 34  -  Studio - MayorTesting:34
  16:27:15.194  Stack End  -  Studio

are plr1, plr2 and plr3 all players if so then just do plr1.Name, plr2.Name or plr3.Name

1 Like

There we go, this was the issue I didn’t have Plr1.Name I just had Plr1, thanks!

You can also use tostring() to convert them to strings.

No no, for example:

local Player = game:GetService("Players").LocalPlayer
Print(Player)

This would error. You can do this:

local Player = game:GetService("Players").LocalPlayer
Print(Player.Name)

Or:

local Player = game:GetService("Players").LocalPlayer
Print(tostring(Player))