Problem: When a player joins a game, a ScreenGui (representing how many coins they have) will be set to 0. If a player has coins and joins a new server, they will have their amount of coins, but the ScreenGui will say 0 until there has been an change in their amount of currency.
EX: I join and earn 50 coins. I leave. I rejoin, I have 0 coins according to the ScreenGui, but I have 50 coins in my player. I know this because I can purchase to destroy a part at the cost of 5 coins. Once I purchase it, the brick destroys, and then ScreenGui updates AFTER the purchase.
Point of Confusion: How do I make it so the ScreenGui shows the correct amount saved once joining?
DataStore Script Local script, located in ServerScriptService
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("CoinSaveSystem") game.Players.PlayerAdded:connect(function(player) local leader = Instance.new("Folder",player) leader.Name = "Currency" local Coins = Instance.new("IntValue",leader) Coins.Name = "Coins" Coins.Value = ds:GetAsync(player.UserId) or 0 ds:SetAsync(player.UserId, Coins.Value) Coins.Changed:connect(function() ds:SetAsync(player.UserId, Coins.Value) end) end) game.Players.PlayerRemoving:connect(function(player) ds:SetAsync(player.UserId, player.Currency.Coins.Value) end)
ScreenGuiScript Local script, located inside the TextBox, located inside a ScreenGui
local coins = game.Players.LocalPlayer.Currency:FindFirstChild("Coins") coins.Changed:Connect(function() script.Parent.Text = "".. coins.Value end)
-I have the TextBox text set to 0 because I new first-time players to start with 0 coins. -Problem is when a player who already has data saved, their TextBox (located inside ScreenGui) will say 0 until there is a change in their data
-Any way to fix my problem? I can provide more information if needed!
The Problem is in the ScreenGui, The Value Uptades whenever its CHANGED, so when you rejoin the value just won't uptade, so what you need to do is:
local coins = game.Players.LocalPlayer.Currency:FindFirstChild("Coins") script.Parent.Text = "".. coins.Value coins.Changed:Connect(function() script.Parent.Text = "".. coins.Value end)
I hope I helped :)