How to make nametag indicate a leaderstat?

Thank you all for responding to previous forums! :smiling_face:

I’ll be as explanatory and clear as possible…

I’ve implemented a nametag into my game and modified it by adding a TextLabel. I was thinking of making the TextLabel display a leaderstat in the game called “Deaths,” which represents the player’s number of deaths.

However, I’m having trouble finding the value. It turns out the “Deaths” value is located in the local player or client, within a folder created with Instance.new called “leaderstats.” Here’s an image to better illustrate:

image

As you can clearly see, that’s where it’s located. The issue here is that I want to accomplish this from the script that inserts the nametag into the character, but I don’t know how to reach the local player and locate that value to display it in the TextLabel. Here’s an image of the location of the script that inserts the nametag into the player:

image

This is what’s inside that script :jigsaw::

wait(0.1)
local char = script.Parent
local player = game.Players:GetPlayerFromCharacter(char)
local groupid = 1234567890 -- ADD YOUR GROUP ID HERE
local UserInputService = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")
local images = {
	
	PC = "rbxassetid://4728059490",
	Mobile = "rbxassetid://4728060256",
	Console="rbxassetid://4728059072"
}
local tag = game.ServerStorage.RankTag:Clone()
local image = game.ServerStorage.DeviceGuiBilboard:Clone()
print("asd")
local name = tag.Frame.plrn 
local label = tag.Frame.plrr
tag.Parent = char:WaitForChild("HumanoidRootPart")
tag.Adornee = char:WaitForChild("HumanoidRootPart")
image.Parent = char:WaitForChild("HumanoidRootPart")
image.Adornee = char:WaitForChild("HumanoidRootPart")

game.ReplicatedStorage.CheckDevice.OnServerEvent:Connect(function(player, device)
	image.ImageLabel.Image = images[device]
	print(device)
end)

name.Text = player.Name
label.Text = player:GetRoleInGroup(groupid)

In summary, I want to locate the value named “Deaths” so that the text on the nametag displays it, something like this:

-- THIS IS AN EXAMPLE DONT USE IT
Ranktag.Textlabel.Text = Deaths.Value

Thank you for your attention, and I look forward to your assistance! :slight_smile:

4 Likes

So you want the deaths stat to display on the deaths text?

2 Likes

yes, but i dont know how to find the value in this script

1 Like

What type of script are you using to check the value?

1 Like

This is the leaderstat script in ServerScriptService:

local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("SuccesValueSaver")
local ds2 = datastore:GetDataStore("CoinsValueSaver")
local ds3 = datastore:GetDataStore("DeathValueSaver")
local ds4 = datastore:GetDataStore("HeartsValueSaver")

players.PlayerAdded:connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player

	local currency1 = Instance.new("IntValue")
	currency1.Name = "Succes"
	currency1.Parent = player.leaderstats
	currency1.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, currency1.Value)
	local currency2 = Instance.new("IntValue")
	local currency3 = Instance.new("IntValue")
	local currency4 = Instance.new("IntValue")

	currency2.Name = "Coins"
	currency2.Parent = player.leaderstats
	currency2.Value = ds2:GetAsync(player.UserId) or 0
	ds2:SetAsync(player.UserId, currency2.Value)
	
	currency3.Name = "Deaths"
	currency3.Parent = player.leaderstats
	currency3.Value = ds3:GetAsync(player.UserId) or 0
	ds3:SetAsync(player.UserId, currency3.Value)
	
	currency4.Name = "Hearts"
	currency4.Parent = player.leaderstats
	currency4.Value = ds4:GetAsync(player.UserId) or 0
	ds4:SetAsync(player.UserId, currency4.Value)
	
	
	currency1.Changed:connect(function()
		ds1:SetAsync(player.UserId, currency1.Value)
	end)
	currency2.Changed:connect(function()
		ds2:SetAsync(player.UserId, currency2.Value)
	end)
	currency3.Changed:connect(function()
		ds3:SetAsync(player.UserId, currency3.Value)
	end)
	currency4.Changed:connect(function()
		ds4:SetAsync(player.UserId, currency4.Value)
	end)
end)
2 Likes

That is what creates the Deaths value right?

2 Likes

Yeah, sorry but roblox won’t let me send the message due to the minimum number of characters.

2 Likes

You can use player.leaderstats.Deaths in the character script to get the value you need

Where i can do that exactly? :sweat_smile:

We are talking about the character of the player, what I want to do is reach the client to find him.

If the name tag is created on the server (NameTagScript) and just change the texts on the name tag then you don’t even need to access the client, just use the name tag directly.
Also you can use the player.leaderstats.Deaths value to get the amount of deaths the player has in the rank tag script
In case you want to update the values, just connect GetPropertyChangedSignal() on the deaths value and update it

1 Like

What happened? Something new? idk

Oh, sorry for the delay. Had to do something else for a few minutes.

Are you trying to achieve this on client or server?

Como dije, estoy tratando de ver dónde puedo conseguir el cliente así llegar a la carpeta leaderstats y blabla

Sorry I don’t really understand that… but if you are doing this on server, try creating a script, not a LocalScript, and change the RunContext of the script (found in the properties) to Client. It should look something like this:
image

Example or reference:

while not game:IsLoaded() do
    task.wait()
end

local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer::Player
local leaderstats = plr:WaitForChild("leaderstats")
local deaths = leaderstats:FindFirstChild("Deaths").Value -- check for the death stat
local deathText = pathToDeathTextHere -- where your death text is (example: script.Parent.DeathText)
deathText = `{tostring(deaths)}`

You need to change the deathText variable to the pathway of your death count (TextLabel) object in the character.

2 Likes

You could use RemoteEvents if this doesn’t work or the method is too complex.

1 Like

It’s not that, I just want in this script located in startercharacterscripts to be able to locate the player client and find the leadersstats folder.

wait(0.1)
local char = script.Parent
local player = game.Players:GetPlayerFromCharacter(char)
local groupid = 1234567890 -- ADD YOUR GROUP ID HERE
local UserInputService = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")
local images = {
	
	PC = "rbxassetid://4728059490",
	Mobile = "rbxassetid://4728060256",
	Console="rbxassetid://4728059072"
}
local tag = game.ServerStorage.RankTag:Clone()
local image = game.ServerStorage.DeviceGuiBilboard:Clone()
print("asd")
local name = tag.Frame.plrn 
local label = tag.Frame.plrr
tag.Parent = char:WaitForChild("HumanoidRootPart")
tag.Adornee = char:WaitForChild("HumanoidRootPart")
image.Parent = char:WaitForChild("HumanoidRootPart")
image.Adornee = char:WaitForChild("HumanoidRootPart")

game.ReplicatedStorage.CheckDevice.OnServerEvent:Connect(function(player, device)
	image.ImageLabel.Image = images[device]
	print(device)
end)

name.Text = player.Name
label.Text = player:GetRoleInGroup(groupid)

Try changing the RunContext to Client in the properties by selecting the script.

If you are just asking how to find it do this right below line 3:

local leaderstats = player.leaderstats
local deathValue = leaderstats.Deaths.Value
2 Likes