How to get Character in server script

So the other day I decided to make a leaderboard for the amount of time you played and it works :slightly_smiling_face:.
But now I am wondering if there is a way can get the first 3 people to give them titles.
I go every thing working but could not figure out how to get the Character in a Server Script so I got stopped at MostPlayedTag.Parent = and I wanted to put = Character.Head but could i did not know how to get the character can anyone help.
Thanks for helping!

Server Script

local DataStoreService = game:GetService("DataStoreService")
local TimeplayedLeaderboard = DataStoreService:GetOrderedDataStore("TimeplayedLeaderboard")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = TimeplayedLeaderboard:GetSortedAsync(false, 5)
		local TimeplayedPage = Data:GetCurrentPage()
		for Rank, data in ipairs(TimeplayedPage) do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local TimePlayed = data.value
			local isOnLeaderboard = false
			for i, v in pairs(game.Workspace.TimePlayedLeaderboard.LeaderboardGui.Holder:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end
			
			if TimePlayed > 0 and isOnLeaderboard == false then
				if Rank <= 10 then
					local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
					newLbFrame.Player.Text = Name
					newLbFrame.TimePlayed.Text = TimePlayed.. "m"
					newLbFrame.Rank.Text = "#"..Rank
					newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + (.085 * #game.Workspace.TimePlayedLeaderboard.LeaderboardGui.Holder:GetChildren()), 0)
					newLbFrame.Parent = game.Workspace.TimePlayedLeaderboard.LeaderboardGui.Holder
					if Rank == 1 then
						local MostPlayedTag = ReplicatedStorage.MostPlayedTag:Clone()
						MostPlayedTag.Parent = 
					end
			end
		end
	end
end)
	
	if not success then
		print(errorMessage)
	end
end

while true do
	
	for _, player in pairs(game.Players:GetPlayers()) do
		TimeplayedLeaderboard:SetAsync(player.UserId, player.leaderstats.Minutes.Value)
	end
	
	for _, frame in pairs(game.Workspace.TimePlayedLeaderboard.LeaderboardGui.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	
	wait(60)
end

Judging by how you have it set up, data.key contains the userid of the player. There’s a function in the player service called GetPlayerByUserId, which returns a player instance of the player with that userid in game. But returns nil if a player with the given userid is not in the server.

So you could do game:GetService("Players"):GetPlayerByUserId(tonumber(data.key)), then check if is not nil, then get the Character property

From this I still do not understand how to get the character.

Right now, from what I can tell, data.key contains the userid of the player. As I said, the Players Service has a function called GetPlayerByUserId, which expects a userid to check if a player ingame has it. It’ll return the player with the same userid or nil if ther ewas no player with the userid. Then, you can get the character

local player = game:GetService("Players"):GetPlayerByUserId(tonumber(data.key))
local character = player and player.Character

if character then
	--code
end

So it goes like this?

if Rank == 1 then
    local player = game:GetService("Players"):GetPlayerByUserId(tonumber(data.key))
    local character = player and player.Character

    if character then
	local MostPlayedTag = ReplicatedStorage.MostPlayedTag:Clone()
        MostPlayedTag.Parent = character.Head
    end

It Fianlly works Thanks so much.

1 Like