I have this script that’s supposed to save the time spent in-game and it awards you a badge for spending a curtain amount of time in-game. Everything was working until recently when it stopped saving, and completely reseted your time when you rejoined.
It was working for a while until one of my friends told me it stopped saving.
My friend had around the same time as his brother (His brother is first place on the leaderboard) but when he rejoined his time just erased.
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")
local badgeservice = game:GetService("BadgeService")
local badgeid = 2124568187
local badgeid2 = 2124568189
local badgeid3 = 2124568190
local badgeid4 = 2124568192
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 >= 1800 then
badgeservice:AwardBadge(plr.UserId, badgeid2)
if plr.leaderstats.Time.Value >= 3600 then
badgeservice:AwardBadge(plr.UserId, badgeid3)
if plr.leaderstats.Time.Value >= 7200 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)
Here is the script that was used, if you see an error please let me know. Thanks.
I’m not too sure how your data was saving if you were not making use of SetAsync() or UpdateAsync(). That is necessary to change the values held within the DataStore. The way I usually save my data is by doing something that resembles the following:
game.Players.PlayerRemoving:Connect(function(plr)
local ID = plr.UserId
local Data
--Set data to whatever you need
DataStore:SetAsync(ID, Data)
end)
I mean, in practice I usually have my PlayerAdded functions followed by my PlayerRemoving functions, but in reality it doesn’t matter a whole lot. As long as you don’t try and do something like put one of them inside the other, it should work just fine either way.
Sorry. I’m completely new to data stores. So something like this?
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")
local badgeservice = game:GetService("BadgeService")
local badgeid = 2124568187
local badgeid2 = 2124568189
local badgeid3 = 2124568190
local badgeid4 = 2124568192
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 >= 1800 then
badgeservice:AwardBadge(plr.UserId, badgeid2)
if plr.leaderstats.Time.Value >= 3600 then
badgeservice:AwardBadge(plr.UserId, badgeid3)
if plr.leaderstats.Time.Value >= 7200 then
badgeservice:AwardBadge(plr.UserId, badgeid4)
end
end
end
end
end
wait(1)
plr.leaderstats.Time.Value = plr.leaderstats.Time.Value + 1
DataStore:SetAsync(TimeSave, Data) ---Updates the data.
until plr == nil
end)
Ok, so that’s similar to your desired result. But the first parameter for SetAsync is your Key, which for me is usually player.UserId. So you would need to change it to:
DataStore:SetAsync(ID, Data)
However, you need to be careful. DataStores have a limit on how many times they can be saved per second. You’d be much better off updating it every minute or so, rather than every time your loop runs.
Well, here’s what I’d personally do. You add a PlayerRemoving function at the end of your script, like in my example a few replies above. That way, the player’s data will save whenever they leave. Then, create another script in ServerScriptService that has an infinite while true do loop and a 60/120/whatever time you want wait in it (but make sure it’s at least a couple of minutes). You can access the same DataStore from any script in your game, as long as you use the same name in :GetDataStore(). Then, you can use this loop to :UpdateAsync() as well.