How to make this coin gui script save with data store

Hello, i made a coin gui script but i’ve tried many ways to make it save with data store but it doesnt work.

the normal script:

local label = script.Parent
local coins = workspace.Coins

label.Text = tostring(coins.Value)

coins:GetPropertyChangedSignal(“Value”):Connect(function()
print(“Change detected”)
label.Text = tostring(coins.Value)

end)

I see a problem here
you should have the value under the player so you can change the data on the server, making it a child of workspace forces you to do it locally if you have more then one player

this script is fine but the DataStoreService isn’t even used
I’ll give you an example

-- "player" variable doesn't exists here, just make one
local IntValue = player.leaderstats.IntValue -- get the IntValue
local DataStore = game:GetService("DataStoreService"): GetDataStore("Saver") -- get the DataStore

IntValue.Value = DataStore:GetAsync(player.Name) -- Set the IntValue's Value

IntValue:GetPropertyChangedSignal("Value"):Connect(function() -- I think you already know this, detects if the value was changed
   if DataStore:GetAsync(player.Name) then -- checks if the saved value exists
 
      DataStore:UpdateAsync(DataStore:GetAsync(player.Name), function() -- if exists, then update the saved value
      return IntValue.Value -- save the value
   end)
else -- if the saved value doesn't exists
   DataStore:SetAsync(player.Name, IntValue.Value) -- just set it
end

Also, you have to access DataStores from server

if this won’t help you, then try to look in DevHub

2 Likes

ohh so should i do coins.Parent = Player?

1 Like

there are two things wrong with this

  1. you are making each datastore based off the player’s name, instead use the player’s userid
  2. you should use UpdateAsync instead of SetAsync

no you should make a folder under the player on join and then create a value under the folder named your currency, a datastore would change the values if there was one

EDIT: this link should help In-Experience Leaderboards | Roblox Creator Documentation

okay but i’m not using leaderboards, i’m using only gui display

I did, and this is only an example

if you don’t want a leaderboard to show up then name the folder something other then “leaderstats”, if the folder is named literally anything else the leaderboard won’t show

1 Like

yea but you still have this one

okay, i’ll try that rn and let you know the result

Also just to add instead of saving values whenever they change you should save it every few minutes and save it when player is leaving and when the server is closing because if values change too quickly request queqe may fill and some requests may be dropped and data wont save

no you should only save at the end, auto saving is bad in general and should only be used in certain cases

1 Like

I mean saving every like 5 minutes won’t do much bad if all players save at the same time because it will protect you from the worst worst worst case

thanks man the steps you gave me actually worked

1 Like

true but that is why we have pcall and xpcall!

1 Like

Yeah but why overload the system for no reason

You should use the player’s User ID incase they change their name. The user id is the same forever, the name does not. If you use their name and they change it, all of their data is lost.

I was talking about UpdateAsync, but you are right