My datastore doesn’t save the data. I will put the script down below.
local DS = game:GetService("DataStoreService"):GetDataStore("SaveMyData")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "id_"..plr.userId
local savevalue = plr.leaderstats.Money
local savevalue2 = plr.leaderstats.Level
local GetSaved = DS:GetAsync(plrkey)
if GetSaved then
savevalue.Value = GetSaved[1]
savevalue2.Value = GetSaved[2]
else
local NumbersForSaving = {savevalue.Value}
DS:GetAsync(plrkey, NumbersForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
DS:SetAsync("id_"..plr.userId, {plr.leaderstats.Cash.Value, plr.leaderstats.Level.Value})
end)
Made a new version - but now it just says that leaderstats isn’t a valid member og player.
local datastore = game:GetService("DataStoreService"):GetDataStore("GameStore") --Call "GameStore" whatever you like
game.Players.PlayerAdded:connect(function(player)
local leaderstats = player.leaderstats
local money = leaderstats.Money
local level = leaderstats.Level
local key = "user-" .. player.userId
local storeditems = datastore:GetAsync(key)
if storeditems then
money.Value = storeditems[1]
level.Value = storeditems[2]
else
local items = {money.Value, level.Value}
datastore:SetAsync(key, items)
end
end)
game.Players.PlayerRemoving:connect(function(player)
local items = {player.leaderstats.Points.Value, player.leaderstats.Wins.Value}
local key = "user-" .. player.userId
datastore:SetAsync(key, items)
end)
I am not sure if that’s the problem, but data store requests occasionally fail, so I suggest you to use pcalls(), and only if it succeded to get data, load it.
For saving for example:
and to load:
local data
local success, err = pcall(function()
data = datastore:GetAsync(key)
end)
if(success) then
money.Value = data [1]
level.Value = data [2]
else
warn(err)
end
local success, err = pcall(function()
datastore:SetAsync(key, items)
end
if(success) then
print("Data saved successfully")
else
warn(err)
end
I don’t think I can post a whole script, but basically, you wrap your GetAsync and SetAsync in the pcall function. And set the data if the sucess is true.
And you should also wrap your set async just incase it fails, you’ll see the error message.
-------------------------------------------------------------------------------------------------------------------
---- Variables ----
local repS = game:GetService("ReplicatedStorage")
local clickEvent = repS.remotes.ClickEvent
local clickEventUpgrade = repS.remotes.ClickEventUpgrade
local tooBroke = repS.remotes.TooBroke
local moneyCostUpdate = repS.remotes.MoneyCostUpdate
---- Cash Add ----
---------------------------------------------------------------------------
---- DataStore ----
local datastore = game:GetService("DataStoreService"):GetDataStore("GameStore") --Call "GameStore" whatever you like
game.Players.PlayerAdded:connect(function(p)
---- Money ----
local stats = Instance.new("Folder", p)
stats.Name = "leaderstats"
local money = Instance.new("IntValue", stats)
money.Name = "Money"
money.Value = 0
---- Level ----
local level = Instance.new("IntValue", stats)
level.Name = "Level"
level.Value = 1
function moneyPay()
money.Value = money.Value + 1 * level.Value
end
clickEventUpgrade.OnServerEvent:Connect(function()
if money.Value >= level.Value * 10 then
money.Value = money.Value - level.Value * 10
level.Value = level.Value + 1
moneyCostUpdate:FireClient(p)
else
tooBroke:FireClient(p)
end
end)
local key = "user-" .. p.userId
local storeditems = datastore:GetAsync(key)
if storeditems then
money.Value = storeditems[1] --Value of the Points, change "points" if you changed line 10
level.Value = storeditems[2] --Value of the Wins, change "wins" if you changed line 14
else
local items = {money.Value, level.Value} --Change Points or Wins if you changed line 10 or 14
datastore:SetAsync(key, items)
end
end)
game.Players.PlayerRemoving:connect(function(player)
local items = {player.leaderstats.Points.Value, player.leaderstats.Wins.Value} --Change Points or Wins if you changed line 10 or 14
local key = "user-" .. player.userId
datastore:SetAsync(key, items)
end)
---------------------------------------------------------------------------