Hello!
I have been experiencing issues with saving leaderstats to a datastore. The leaderstats successfully save, but when a player leaves the game and rejoins the same specific server that they previously left, the leaderstats don’t save nor update.
I am currently using the following two scripts in my game, both scripts are in ServerScriptService:
This script should create three leaderstats, Credits, Playtime, and Prizes Won. It also increases the “Playtime” value by 1 every minute.
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Credits"
money.Parent = leaderstats
local playtime = Instance.new("IntValue")
playtime.Name = "Playtime"
playtime.Parent = leaderstats
local prizesWon = Instance.new("IntValue")
prizesWon.Name = "Prizes Won"
prizesWon.Parent = leaderstats
while true do
wait(60)
playtime.Value = += 1
end
end)
This is the datastore script. I’ve added a new identical section for every new leaderstat I’ve added.
local DataStoreService = game:GetService("DataStoreService")
local leaderstatsData = DataStoreService:GetDataStore("LeaderstatsData") --First Save (A)
local leaderstatsData2 = DataStoreService:GetDataStore("LeaderstatsData2") --Second Save (B)
local leaderstatsData3 = DataStoreService:GetDataStore("LeaderstatsData3") --Third Save (C)
game.Players.PlayerAdded:Connect(function(player)
local playerData = leaderstatsData:GetAsync(player.UserId) --A
local playerData2 = leaderstatsData2:GetAsync(player.UserId) --B
local playerData3 = leaderstatsData3:GetAsync(player.UserId) --C
if not playerData then --A
playerData = {
Credits = 300
}
leaderstatsData:SetAsync(player.UserId, playerData)
end
if not playerData2 then --B
playerData2 = {
Playtime = 0
}
leaderstatsData2:SetAsync(player.UserId, playerData2)
end
if not playerData3 then --C
playerData3 = {
["Prizes Won"] = 0
}
leaderstatsData3:SetAsync(player.UserId, playerData3)
end
player.leaderstats.Credits.Value = playerData.Credits --A
player.leaderstats.Playtime.Value = playerData2.Playtime --B
player.leaderstats["Prizes Won"].Value = playerData3["Prizes Won"] --C
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerData = { --A
Credits = player.leaderstats.Credits.Value
}
leaderstatsData:SetAsync(player.UserId, playerData)
local playerData2 = { --B
Playtime = player.leaderstats.Playtime.Value
}
leaderstatsData2:SetAsync(player.UserId, playerData2)
local playerData3 = { --C
["Prizes Won"] = player.leaderstats["Prizes Won"].Value
}
leaderstatsData3:SetAsync(player.UserId, playerData3)
end)
This script should initialize the values to certain values (300, 0, 0) if a player doesn’t have any data. Whenever a player leaves and rejoins, their data is set to (0, 0, 0), possibly meaning that the script didn’t work at all.
What might be causing the issue? I am also trying to reduce the amount of datastore calls as my game is having issues related to datastores.
Thank you!
Note: I am not too experienced with programming, I might have made interpretation errors with the code.