When player dies, the GUI doesn't set the player's currency amount. It instead sets it to the text

Hi, when the player joins the game, the player can see their currency value. If the player dies in-game, it defaults the GUI text. Here is the code

PlayerDatastore2

local DataStore2 = require(game.ServerScriptService.DataStore2)

local defaultCashValue = 0

game.Players.PlayerAdded:Connect(function(plr)
	local coinsDataStore = DataStore2("Coins", plr)
	

	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	
	local function coinsUpdated(updatedValue)
		cash.Value = coinsDataStore:Get(updatedValue)
		game.ReplicatedStorage.UpdateClientCurrency:FireClient(plr, coinsDataStore:Get(defaultCashValue))
	end
	
	coinsUpdated(defaultCashValue)
	
	coinsDataStore:OnUpdate(coinsUpdated)
	
	
	
end)

GUI Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.UpdateClientCurrency.OnClientEvent:Connect(function(updatedValue)
	script.Parent.Text = "$" .. updatedValue
end)

Thanks for the help!

1 Like

Make sure to disable the ResetOnSpawn property of the ScreenGui

1 Like

Another explanation for this problem: your Gui script doesn’t have an initial state setter, so the Gui text won’t be corrected to the currency value immediately. You can still work with ResetOnSpawn as false (better not to for your case though), but you also need to update the cash initially and not just if the RemoteEvent receives OnClientEvent.

1 Like