I have a datastore leaderstats and its not working
I tried all things but nothing changed can someone help here is the code:
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerMoney")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderboard"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = playerData:GetAsync(player.UserId.."-money")
end)
if success then
money.Value = data
else
print("Error while getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
playerData:SetAsync(player.UserId.."-money", player.leaderstats.Money.Value)
end)
if success then
print("Data successfully saved!")
else
print("There was an error while saving the data")
warn(errormessage)
end
end)
It is simple, go to game settings, go somewhere, if you see “Allow API Services”, turn that on. It will make the datastore have access to your leaderstats and saving it, hopefully it fixes it.
Found the issue.
It is because the server was closing before the data could be saved, add this to the end of the script and move the data saving to a function.
game:BindToClose(function()
--call data saving function here for each player
wait(10) --the max is 30 seconds
end)
Also at line 30, you do player.leaderstats but the name of the folder containing the data is called leaderboard.
Well, i didn’t do it with that bindToClose function and it still saved my data in a game i developed at, his saying his data won’t save, if you stop in run mode it still counts as exitting the game thus PlayerRemoved, it is probably because the API mode he didn’t turn on which is compatible with datastore.
The main issue is that he has tried to save player.leaderstats.Money.Value but the data container is named leaderboard instead which is why it is not saving.
It is also better practice to use BindToClose just incase.
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerMoney")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderboard"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = playerData:GetAsync(player.UserId.."-money")
end)
if success then
money.Value = data
else
print("Error while getting your data")
warn(errormessage)
end
end)
local function SavePlayerData(player)
local success, errormessage = pcall(function()
playerData:SetAsync(player.UserId.."-money", player.leaderboard.Money.Value)
end)
if success then
print("Data successfully saved!")
else
print("There was an error while saving the data")
warn(errormessage)
end
end
game.Players.PlayerRemoving:Connect(SavePlayerData)
game:BindToClose(function()
for _,Player in pairs(game:GetService("Players"):GetPlayers()) do
SavePlayerData(Player)
end
wait(5)
end)
I also named the leaderstats to the correct naming so it will appear on the playerlist
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerMoney")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderboard"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = playerData:GetAsync(player.UserId.."-money")
end)
if success then
money.Value = data
else
print("Error while getting your data")
warn(errormessage)
end
end)
local function SavePlayerData(player)
local success, errormessage = pcall(function()
playerData:SetAsync(player.UserId.."-money", player.leaderstats.Money.Value)
end)
if success then
print("Data successfully saved!")
else
print("There was an error while saving the data")
warn(errormessage)
end
end
game.Players.PlayerRemoving:Connect(SavePlayerData)
game:BindToClose(function()
for _,Player in pairs(game:GetService("Players"):GetPlayers()) do
pcall(function()
SavePlayerData(Player)
end)
end
wait(5)
end)