Stats displaying GUI not updating

I am working on a main menu where the player can view their kills, deaths, K/D, cash, and equipped weapon. I created a function to set the values and activated the function when the player joins and also when the player dies, which is when the menu is supposed to appear. It doesn’t work at all, the text doesn’t change under either circumstance (player dies/joins). What am I doing wrong?

local cashL = script.Parent.Main.StatsFrame.CashAmount
local killsL = script.Parent.Main.StatsFrame.KillsAmount
local deathL = script.Parent.Main.StatsFrame.DeathAmount
local kdrL = script.Parent.Main.StatsFrame.KDRAmount
local weaponL = script.Parent.Main.StatsFrame.WeaponName
local players = game:GetService("Players")
local player = players.LocalPlayer
local stats = player.playerStats

function updateStats()
	cashL.Text = "$"..stats.Cash.Value
	killsL.Text = stats.Kills.Value
	deathL.Text = stats.Deaths.Value
	if equippedTool == nil then
		equippedTool = "Dagger"
		weaponL.Text = equippedTool
	else
		weaponL.Text = equippedTool
	end
	if stats.Deaths.Value == 0 then
		kdrL.Text = stats.Kills.Value/1
	else
		kdrL.Text = stats.Kills.Value/stats.Deaths.Value
	end
end


players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)		
		updateStats()
		character:WaitForChild("Humanoid").Died:Connect(function()
			updateStats()
		end)
	end)
end)
1 Like

Just a quick look at the first line of the function.

cashL.Text = "$"..stats.Cash.Value

The logic behind it is that u are only changing the value of variable instead of an actual gui. How to solve the problem? - u just need to call cashL by it’s full path, like that:

script.Parent.Main.StatsFrame.CashAmount.Text = "$"..tostring(stats.Cash.Value) -- idk might needed tostring, if the value is a number
2 Likes

tables and Instances are passed by reference and aren’t copied unlike strings and integers. The OP is not changing the value of the variable instead of an actual GUI, in fact, this is redundant.
You don’t need tostring for double values for concatination, it’s automatcially converted to strings.

print('$' .. 1) --> $1

As for the OP, what text didn’t change? Can you show where are these in explorer? (I guess these are TextLabels) and can you please screenshot (or video) of the issue?
and why are you diving by 1 on this line when you can achieve the same thing about dividing by 1?

3 Likes

playerStats is a folder that I created under the player to hold the values that I want. Kind of like leaderstats, but without the leaderboard. I am trying to change cashL’s text to the value of the player’s cash which is a value under playerStats, and I’m pretty sure this should work fine.

Nothing changed, and there are no errors. The text remains the same as default and btw, I divided by 1 if the player doesn’t have any deaths. In a KDR, you divide kills by deaths and you cannot divide by 0. So, I made sure that if the player has 0 deaths, then it should divide by 1, which is how your KDR is displayed in all combat games if you haven’t died yet.

As for the location of each of these, I mentioned the values are stored under a folder created under the player named playerStats. What I am trying to change is the text labels in my GUI, so they display the player’s stats correctly. They are all under a frame, which you can get to like this:
game>startergui>MainGui>Main>StatsFrame>(text label)
this code was written in a local script stored under the screen gui (MainGui)

I’m not sure why your gui isn’t updating with the new values. I have made a replica to take a look at your code and mine works perfectly

1 Like

it is really strange, because I don’t have any errors and it seems like it should work. I will double-check spelling and things that may be case sensitive.

Try printing something inside of the updateStats() function, to see if it is even being ran/reached.

And also, this:

Seemed to be irrelevant, since PlayerObject.CharacterAdded runs when a player’s character is added, meaning that if the first one died, a new one would be made.(Meaning this would fire for each of the character’s death too.)

@toe_s

1 Like

Oh, that would make sense lol. I tried printing, but nothing happens, so as I expected the function isn’t running at all.

Where would the problem be here though?

players.PlayerAdded:Connect(function(player)	
	player.CharacterAdded:Connect(function(character)
		updateStats()
	end)
end)
1 Like

@PhoenixRessusection

On a side note, I changed it so when you click a button it updates and everything works properly, so something about a character coming into the game isn’t having it

1 Like

Try printing something in the game.Players.PlayerAdded function, if it prints nothing then i suppose what i expected seems to be true…

Like this:

players.PlayerAdded:Connect(function(player)	
    print("yes")
	player.CharacterAdded:Connect(function(character)
		updateStats()
	end)
end)

Nothing in the output, how can I fix this?

As i expected… What is happening is that, that function, isn’t running(Obvious…), its happening since you already was added to game.Players before that function could even detect your join.(Ok… i explained very badly.)

Firstly, we are going to have a use case for this:

Since i didn’t see you using it, so try using this code:

local cashL = script.Parent.Main.StatsFrame.CashAmount
local killsL = script.Parent.Main.StatsFrame.KillsAmount
local deathL = script.Parent.Main.StatsFrame.DeathAmount
local kdrL = script.Parent.Main.StatsFrame.KDRAmount
local weaponL = script.Parent.Main.StatsFrame.WeaponName
local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local stats = plr.playerStats

function updateStats()
	cashL.Text = "$"..stats.Cash.Value
	killsL.Text = stats.Kills.Value
	deathL.Text = stats.Deaths.Value
	if equippedTool == nil then
		equippedTool = "Dagger"
		weaponL.Text = equippedTool
	else
		weaponL.Text = equippedTool
	end
	if stats.Deaths.Value == 0 then
		kdrL.Text = stats.Kills.Value/1
	else
		kdrL.Text = stats.Kills.Value/stats.Deaths.Value
	end
end


plr.CharacterAdded:Connect(function(char)
	updateStats()
end)

Also, i just noticed that “equippedTool”, seems to not even exist, was this intended or this isn’t the/a full script?

This is apart of a 200+ line script and I realized that player was a repeating variable, so I changed that a little bit ago, and now that I removed the PlayerAdded function, it still doesn’t work. This is the only part that references the character too, if that matters.
edit: equippedTool is apart of my weapon store where player’s can buy and equip weapons, and equippedTool is changed everytime a new weapon is equipped

1 Like

@PhoenixRessusection

this is my current local script (edit: weird formatting)

local cashL = script.Parent.Main.StatsFrame.CashAmount

local killsL = script.Parent.Main.StatsFrame.KillsAmount

local deathL = script.Parent.Main.StatsFrame.DeathAmount

local kdrL = script.Parent.Main.StatsFrame.KDRAmount

local weaponL = script.Parent.Main.StatsFrame.WeaponName

local stats = player.playerStats

function updateStats()

print("update")

cashL.Text = "$"..stats.Cash.Value

killsL.Text = stats.Kills.Value

deathL.Text = stats.Deaths.Value

if equippedTool == nil then

equippedTool = "Dagger"

weaponL.Text = equippedTool

else

weaponL.Text = equippedTool

end

if stats.Deaths.Value == 0 then

kdrL.Text = stats.Kills.Value/1

else

kdrL.Text = stats.Kills.Value/stats.Deaths.Value

end

end

player.CharacterAdded:Connect(function(character)

updateStats()

end)

@PhoenixRessusection

Solved my issue by changing the code at the bottom to this:

player.Character:WaitForChild("Humanoid").Died:Connect(function()

     wait(6)

     updateStats()

end)

if player.Character:WaitForChild("Humanoid") ~= nil then

     updateStats()

end
1 Like