How to Reset Coins TextLabel When Player Dies

I know this seems stupid but ive been all over the forum and couldnt get it working. I have a coins value stored in the player and after the player fully respawns after dieing i want the textlabel to be set back to the coins value. I have tried character added and all of that and i cant figure out what im doing wrong

Just use a loop to constantly set it to the coins value:

coins = Coin value object
label = the coins text label

while wait() do
	label.Text = coins.Value
end

Or you can use a function every time the value changes:

coins:GetPropertyChangedSignal("Value"):Connect(function(value)
	label.Text = value
end)

I haven’t tested these but I’m sure they would work.

Exactly as Gamer said and you can also use a RunService loop as I think it’s better practice than Wait() and updates every frame rendered but it’s up to you. :slightly_smiling_face:

local RunService = game:GetService("RunService")
RunService.RenderStepped:Connect(function()
	Label.Text = Value
end
local players = game:GetService("Players")
local player = players.LocalPlayer

player.CharacterAdded:Connect(function (character)
	TextLabel.Text = coins.Value
end)

Place code in a local script. Parent it somewhere within the GUI (make sure the references are valid).

local player = game:GetService("Players").LocalPlayer
local gui = script.Parent
local coinsText = gui:WaitForChild("CoinsText")
local coins = player:WaitForChild("Coins")

coinsText.Text = tostring(coins.Value)

coins:GetPropertyChangedSignal("Value"):Connect(function()

coins.Text = tostring(coins.Value)

end)

We initially set the text to the current value of coins, then we connect a function so that it updates every time the value is changed.

Additionally, ScreenGui’s have a property known as ā€œResetOnSpawnā€. Assuming you don’t have a health bar in the same gui, you could consider setting this to false.

sorry for indentation, I wrote this on Mobile

1 Like

Thats not what i meant. I already have a script for that. It doesn’t change when the player dies. Read the title

or just prevent the label from resetting itself in the first place

there is a property for this under the main gui.

1 Like

I could do that but I just want this script for resetting everything after respawn like applying the players avatar items, etc
Heres the script. It prints but doesnt update textlabel

local Players = game:GetService("Players")

local function onCharacterAdded(character)
print(character.Name .. " has spawned")
local player = Players:GetPlayerFromCharacter(character)
player.PlayerGui.Main.Coins.TextLabel.Text = (player.Coins.Value)
end

local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

May I see your code so I can get a better understanding?

1 Like

I just posted it in a reply to juicy fruit

Ok I’ve found the issue.

The characterAdded event is firing before the gui resets itself. Basically, the serverScript is getting the gui just before it deletes itself and gets replaced. (Resulting in a return of nil)

Whilst I don’t recomment using a serverScript to access the playerGui, here is the updated code

local Players = game:GetService("Players")

local function onCharacterAdded(character)
	print(character.Name .. " has spawned")
	local player = Players:GetPlayerFromCharacter(character)
	wait(.25)
	player.PlayerGui:WaitForChild("Main").Coins.TextLabel.Text = player.Coins.Value
	
end

local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

I do recommend just setting the value on the client with a single line though

script.Parent.Coins.TextLabel.Text = game.Players.LocalPlayer.Coins.Value
1 Like

Do you want it to save the coins value upon player death or do you want it to reset to 0?

you could do something like

local humanoid = SetHumanoidHere
local textlabel = SetTextlabel Here

humanoid.Died:Connect(function() --Detects when player dies
wait(4) --wait for player to respawn (change number to respawn time)
textlabel.text = player.coin.value --changes the text to coin value

There’s a property on the GUI called ResetOnSpawn, try disabling it

Thank u so muchšŸ™ƒ I had no idea local scripts ran every time a player respawns

1 Like

The local script runs again because the Gui itself is being replaced by a copy.

Glad it worked

1 Like