UI local script not working correctly

I made a local script in a text label with a changed function in it to display a player’s stats.
The problem is that it would not update when they joined the game, it would only update when the amount changed. This led to players thinking their stats were reset. I added in a player added function to update the stats when they join and I thought it was working, but when testing in a live server, it is adding all players stats together and displaying them incorrectly. (edit: to be clear, if 1 player had 5000 endurance and another player had 5000 it would show that player 1 had 10,000 endurance.)

Here is the script I am using now:

local Players = game:GetService("Players")
local player = Players.LocalPlayer

player.leaderstats.End.Changed:connect(function()
	script.Parent.Text = player.leaderstats.End.Value
end)

local function onPlayerAdded(player)
	script.Parent.Text = player.leaderstats.End.Value
end

game.Players.PlayerAdded:Connect(onPlayerAdded)
for _,player in pairs(game.Players:GetPlayers()) do
    onPlayerAdded(player)
end

I have tried adding a waitforchild for leaderstats in the function onPlayerAdded and I tried using a script and getting the player from script.Parent.Parent etc., but neither of those things worked and it is hard to test because I have to have multiple people on to see if it is working.

You could try GetPropertyChangedSignal, that worked for me.

It would be like:

player.leaderstats.End:GetPropertyChangedSignal("Value"):Connect(function()
script.Parent.Text = player.leaderstats.End.Value

end)

1 Like

That works great. I was doing some more investigating and apparently it was updated the stats with the last person to join the game, not adding them together like I originally thought.

edit: I thought that did the trick, but it still is not updating when they join the game on a live server, only when I test in studio.

This is where I got the idea for the last part of the script, but I think that is where my problem is:

1 Like

LocalScripts run on the client. There is no need to use PlayerAdded in the LocalScript. When the player joins, the LocalScript runs, but only to that client, not other players.

local Players = game:GetService("Players")
local player = Players.LocalPlayer

script.Parent.Text = -- Put in the leaderstat value

player.leaderstats.End.Changed:connect(function()
	script.Parent.Text = player.leaderstats.End.Value
end)

Remember, LocalScripts run Lua code on a client connected to a Roblox server.

the reason why your script is adding it incorrectly is because of the PlayerAdded event. This will run whenever any player joins. That is why it was working on studio, you where the only person. But if another person joins, the script will update to the new player’s value

the reason why it is displaying the latest person’s value is because of the playerAdded event. It will fire whenever any player joins.

srry for the last post, deleted it and can’t get it back :sweat_smile:

You are correct!

Thank you.

I don’t know why I made it so much harder.
Now to go change a few hundred scripts again…