Hi there! I have been trying to make a money system. It’s kind of difficult for me since I am a rookie on scripting. I am not sure if I did my script correctly. I have an issue because the Money GUI is not updating! Even when I re-join! Here is the video:
This is the code that I probably did wrong:
local DataStoreService = game:GetService("DataStoreService")
local Money = DataStoreService:GetDataStore("Money")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Money"
Cash.Parent = leaderstats
local Id = "Player_"..player.UserId
local data
local success, errormessage = pcall(function()
data = Money:GetAsync(Id)
end)
if success then
Cash.Value = data
end
game.StarterGui.CURRENCY.CASH.VALUE.Text = (Cash.Value.."$")
end)
Players.PlayerRemoving:Connect(function(player)
local Id = "Player_"..player.UserId
local success, errormessage = pcall(function()
Money:SetAsync(Id, player.leaderstats.Money.Value)
end)
if success then
print("Data successfully saved!")
else
print("Something went wrong while saving data...")
warn(errormessage)
end
end)
Side note: Optional help but is there a way so if the value is bigger then 1,000 it will turn to 1K or like 1,000,000 to 1M please? Thanks!
The problem is you are changing the StarterGui when you should be changing the player’s PlayerGui instead.
Additionally, you should create a .Changed connection on the client for the IntValue so you can update the GUI each time the player’s money is changed.
I suggest you use something like this in a local script inside the text gui you want to change:
local player = game.Players.LocalPlayer
local StatFolder = player:WaitForChild("leaderstats") -- Change to your stat name
local Stat = StatFolder:WaitForChild("Money") -- Change to your currency name
script.Parent.Text = Stat.Value.."$"
Stat.Changed:Connect(function()
script.Parent.Text = Stat.Value.."$"
end)
You should only change the player gui using local scripts running on the client. Place the code I gave you above in a local script in the text element you want to change. Hope this helps.
local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")
local Money = leaderstats:WaitForChild("Money")
while true do
wait()
script.Parent.Text = tostring(Money.Value) -- Converting IntValue to string
end
All this is doing is changing the text label in the StarterGui. When a player loads into the game, Roblox takes a copy of the GUIs from StarterGui and puts them into PlayerGui. Each player has their own GUI. You could find it by doing Player:WaitForChild("PlayerGui").
To correct your mistake; you will want to add a LocalScript, a script that will only affect what the player sees, and put it in the TextLabel that displays the money.
local Player = game:GetService("Players").LocalPlayer
local Money = Player:WaitForChild("leaderstats"):WaitForChild("Money")
local Gui = script.Parent
Gui.Text = tostring(Money.Value)
Money:GetPropertyChangedSignal("Value"):Connect(function()
Gui.Text = tostring(Money.Value)
end)
Are you checking if the value has changed to update the GUI when it changes? If you didn’t, thats why, if you did, I’m blind.
About the side note, I don’t know how to right now, but I did something like that once.
People are saying the same thing, it’s kind of obvious, but don’t worry, there are always dumb fails.
local money = player:WaitForChild("leaderstats"):WaitForChild("Money")
local text = script.Parent
local function update()
text.Text = "$" .. money.Value
end
update()
money.Changed:Connect(update)
I have fixed it everyone! Thanks @EmbatTheHybrid for the help and the rest of you! ^