I’m trying to get my data store to save when the player leaves the game, I have 2 different scripts both in ServerScriptService. One holds the data store, this one:
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")
local badgeservice = game:GetService("BadgeService")
local badgeid = 2124575491
local badgeid2 = 2124565356
local badgeid3 = 2124565358
local badgeid4 = 2124565361
game.Players.PlayerAdded:Connect(function(plr)
local m = 0
local leaderstats = Instance.new("Folder")
leaderstats.Parent = plr
leaderstats.Name = "leaderstats"
local gems = Instance.new("IntValue")
gems.Parent = leaderstats
gems.Name = "Time"
local data
local success, err = pcall(function()
data = ds1:GetAsync(plr.UserId)
end)
if success then
gems.Value = data
end
repeat wait()
m = m + 1
if m == 2 then
m = 0
if plr.leaderstats.Time.Value >= 600 then
badgeservice:AwardBadge(plr.UserId, badgeid)
if plr.leaderstats.Time.Value >= 3600 then
badgeservice:AwardBadge(plr.UserId, badgeid2)
if plr.leaderstats.Time.Value >= 18000 then
badgeservice:AwardBadge(plr.UserId, badgeid3)
if plr.leaderstats.Time.Value >= 36000 then
badgeservice:AwardBadge(plr.UserId, badgeid4)
end
end
end
end
end
wait(1)
plr.leaderstats.Time.Value = plr.leaderstats.Time.Value + 1
until plr == nil
end)
The other one makes the data store save. This one:
local p = game:GetService("Players"):GetPlayerByUserId(1)
game.Players.PlayerRemoving:Connect(function(plr)
local leaderstats = Instance.new("Folder")
local a = plr.leaderstats.Time.Value
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")
ds1:UpdateAsync(p, a) ---Saves the data.
end)
For some reason it won’t save, please let me know what I’m doing wrong. Thanks.
There’s no point of making a new script to save and get data, put them all in one script.
Next your making a new leaderstats folder, and I assume LUA doesnt know which one to save. plr already has a leaderstats folder so making a new one is useless.
Next, use SetAsync() instead of UpdateAsync() I dont know why but its just better.
If you put them all in the same script, the saving data script should work if you run a pcall().
Make sure to set leaderstats = to the existing leaderstats
The reason I’m using another script is it’s less mess and it’s easier to do. I tried using SetAsync() but I didn’t know how to use it very well. So I used UpdateAsync() instead.
local p = game:GetService("Players"):GetPlayerByUserId(1)
game.Players.PlayerRemoving:Connect(function(plr)
local leaderstats = Instance.new("Folder")
local a = plr.leaderstats.Time.Value
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")
ds1:SetAsync(p, a) ---Saves the data.
end)
Once again remove the leaderstats folder. Player already has one assuming that you have a leaderstats system. local leaderstats = plr.leaderstats
Two, use a pcall to set the async since ROBLOX Api’s can still fail.
local success, err = pcall(function()
--- blah blah do stuff here
)
Three, you are saving the player’s data that is leaving, not the one that you defined. Why are you using p if that’s only saving data for that specific player?
Firstly, I would also suggest putting all the code in one script, so its easier to manage and handle, unless you absolutely need to.
When you run the function connected with the PlayerRemoving event, you save the value of Time, yet you get the Value of gems when the player joins again. Could this be a possible mistake? You should try to save the gems Value if you want to retrieve it the next time the player joins the game.
Additionally, UpdateAsync accepts two parameters, the key it should update data in, and a function that actually updates the data (which it runs on its own). See here for more info. However, I think it makes little difference to use UpdateAsync in this case, and you could use SetAsync here. I made example code below to use as a guideline.
game.Players.PlayerRemoving:Connect(function(Player)
local leaderstats = Player.leaderstats
local Gems_Value = leaderstats.Gems.Value
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")
local success, err = pcall(function()
ds1:SetAsync(Player.UserId, Gems_Value)
end)
end)
@Syntheix UpdateAsync is actually more reliable of a saving method compared to SetAsync when use correctly. If you would like to learn more please read this great article.
This would likely work if you were planning to save the Gems the player has. However, you save the Time in your original post, so I’m not sure if this would be what you need.
It’s made so I can award time badges, in other words, award a badge for staying in the game a curtain amount of time. I’m trying to make it save so you don’t have to restart everytime you rejoin.
I think I did something wrong, for some reason it won’t save when you leave. It just sets back to 0. I assume I put it in the wrong place.
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")
local badgeservice = game:GetService("BadgeService")
local badgeid = 2124575491
local badgeid2 = 2124565356
local badgeid3 = 2124565358
local badgeid4 = 2124565361
game.Players.PlayerAdded:Connect(function(plr)
local m = 0
local leaderstats = Instance.new("Folder")
leaderstats.Parent = plr
leaderstats.Name = "leaderstats"
local gems = Instance.new("IntValue")
gems.Parent = leaderstats
gems.Name = "Time"
local data
local success, err = pcall(function()
data = ds1:GetAsync(plr.UserId)
end)
---Where is saves.
game.Players.PlayerRemoving:Connect(function(Player)
local leaderstats = Player.leaderstats
local timevalue = leaderstats.Time.Value
local success, err = pcall(function()
ds1:SetAsync(Player.UserId, timevalue)
end)
end)
---End of where it saves.
if success then
gems.Value = data
end
repeat wait()
m = m + 1
if m == 2 then
m = 0
if plr.leaderstats.Time.Value >= 600 then
badgeservice:AwardBadge(plr.UserId, badgeid)
if plr.leaderstats.Time.Value >= 3600 then
badgeservice:AwardBadge(plr.UserId, badgeid2)
if plr.leaderstats.Time.Value >= 18000 then
badgeservice:AwardBadge(plr.UserId, badgeid3)
if plr.leaderstats.Time.Value >= 36000 then
badgeservice:AwardBadge(plr.UserId, badgeid4)
end
end
end
end
end
wait(1)
plr.leaderstats.Time.Value = plr.leaderstats.Time.Value + 1
until plr == nil
end)