so when a player joins a game, if they have cash already saved; i want to load their cash. if they have no data then i want their cash to be 100,000. im trying to load and save the players cash.
when i join the game the cash ends up being 0 instead of 100,000 and yeah that’s the problem. i only started working with datastore today so i appreciate any help tysm!!
code:
local dss = game:GetService("DataStoreService")
local cash_data = dss:GetDataStore("cash_data")
--player join
function module.LeaderboardandLoadData(p:Player)
local leaderstats = Instance.new("Folder", p)
leaderstats.Name = "leaderstats"
local Cash = Instance.new("IntValue", leaderstats)
Cash.Name = "Cash"
local data
local success, err = pcall(function()
data = cash_data:GetAsync(p.UserId)
end)
if success then
Cash.Value = data
else
Cash.Value = 10000
end
end
--player leave
function module.SavePlayerData(p)
local leaderstats = p:WaitForChild("leaderstats")
local Cash = leaderstats:FindFirstChild("Cash")
local success, err = pcall(function()
cash_data:SetAsync(p.UserId, Cash.Value)
end)
if success then
print("data saved for: "..p.Name)
else
warn("error saving data for: "..p.Name)
end
end
If a :GetAsync() call fails you want to try again.
However, you can run into API Rate limits if not properly handled, but in this case it should be fine and you should not have to worry about rate limits.
yeah i tried testing again, publishing and even tried by playing roblox itself, it just sets my cash as 0 when i join. its really weird because i did a 2 player test. one player has 10,000; the other has 0.
i renamed the data store variable to like “Jjjj” or something and named it back and its loading in the cash fine now. i just have an issue with saving. it doesnt save the cash unfortunately.
local cash_data = dss:GetDataStore("cashData")
it prints saying the data was saved but it wasnt thats the thing
When the player has no data, the getasync call won’t fail/error, so success will be true. You would have to check explicitly that they have no data, eg:
local success, err = pcall(function()
data = cash_data:GetAsync(p.UserId)
end)
if success then
Cash.Value = data or 100000
-- or use an if-statement (if data then value = data else value = 100000)
else
-- handle the request failing
end
--player leave
function module.SavePlayerData(p)
local leaderstats = p:WaitForChild("leaderstats")
local Cash = leaderstats:FindFirstChild("Cash")
local success, err = pcall(function()
cash_data:SetAsync(p.UserId, Cash.Value)
end)
if success then
print("data saved for: "..p.Name)
else
warn("error saving data for: "..p.Name)
end
end
--player leave
function module.SavePlayerData(p)
local leaderstats = p:WaitForChild("leaderstats")
local Cash = leaderstats:FindFirstChild("Cash")
local success, err = pcall(function()
cash_data:SetAsync(p.UserId, Cash.Value)
end)
if success then
print("data saved for: "..p.Name)
else
warn("error saving data for: "..p.Name)
end
end
function module.ExtendServerExitTime()
warn("WARNING: server shutting down! saving player data...")
task.wait(3)
warn("server shutting down!")
end
The problem is that you aren’t saving the players data when the server shuts down.
Which is what game:BindToClose() does.
Your :BindToClose() should look like
game:BindToClose(function()
for _, Player in game.Players:GetPlayers() do
leaderboard_module.SavePlayerData(Player)
end
end)
Saving 2 times on the PlayerRemoving and Shutting down IS NOT a good practice, Extending the time should actually fix that without doing unnecessary saves.
function module.SavePlayerData(p)
local leaderstats = p:FindFirstChild("leaderstats")
local Cash = leaderstats:FindFirstChild("Cash")
local success, err = pcall(function()
cash_data:SetAsync(p.UserId, Cash.Value)
end)
if success then
print("data saved for: "..p.Name)
else
warn("error saving data for: "..p.Name.." Error:", err)
end
end
You also gotta make sure you have the API Services enabled so the data can be saved.
i ran the code you provided and its printing saying that the data was saved in the output but when i join back it just reverts to the initial 10k. i have API services enabled btw