DataStore request was added to queue. (lost all data)
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("Dvddlayer")
local function saveDate(player)
local tableToSave = {
player.leaderstats.Wins.Value;
--player.Stages.Value;
player.TimePlayed.Value
}
local success, err = pcall(function()
datastore:SetAsync(player.UserId, tableToSave)
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
wins.Value = 0
--local Stage = Instance.new("IntValue")
--Stage.Name = "Stages"
--Stage.Parent = player
--Stage.Value = 0
local TimePlayed = Instance.new("IntValue")
TimePlayed.Name = "TimePlayed"
TimePlayed.Parent = player
TimePlayed.Value = 0
local data
local success, err = pcall(function()
data = datastore:GetAsync(player.UserId)
end)
if success and data then
wins.Value = data[1]
--Stage.Value = data[2]
TimePlayed.Value = data[2]
else
print("The player has no data")
end
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
saveDate(player)
end)
if success then
print("data has been saved")
else
print("data has not been saved!")
end
end)
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveDate(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("SavedTools")
local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("SieDffsagtfa32")
game.Players.PlayerAdded:Connect(function(Player)
local ToolData = SaveData:GetAsync(Player.UserId)
local BackPack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")
if ToolData ~= nil then
for i, v in pairs(ToolData) do
if ToolFolder:FindFirstChild(v) and BackPack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
ToolFolder[v]:Clone().Parent = BackPack
ToolFolder[v]:Clone().Parent = StarterGear
end
end
end
Player.CharacterRemoving:Connect(function(Character)
Character:WaitForChild("Humanoid"):UnequipTools()
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
local ToolTable = {}
for i, v in pairs(Player.Backpack:GetChildren()) do
table.insert(ToolTable, v.Name)
end
if ToolTable ~= nil then
SaveData:SetAsync(Player.UserId, ToolTable)
end
end)
I have problem with datastore all the time can somebody help me with that.
The code is all messed up; I can’t really understand it. But it seems that you are creating an event for leaving the game and looping the players every time they join the server. You need to only have one event for leaving the game, so you need to put that outside the PlayerAdded function. Also, why do you need to loop the players and save their data every time someone joins? I assumed this was the case seeing as the end of the PlayerAdded function was at the very end.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
...
end)
Players.PlayerRemoving:Connect(function(player)
...
end)
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("Dvddlayer")
local Players = game:GetService("Players")
local function saveDate(player)
local tableToSave = {
player.leaderstats.Wins.Value;
--player.Stages.Value;
player.TimePlayed.Value
}
local success, err = pcall(function()
datastore:SetAsync(player.UserId, tableToSave)
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!!")
warn(err)
end
end
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
wins.Value = 0
--local Stage = Instance.new("IntValue")
--Stage.Name = "Stages"
--Stage.Parent = player
--Stage.Value = 0
local TimePlayed = Instance.new("IntValue")
TimePlayed.Name = "TimePlayed"
TimePlayed.Parent = player
TimePlayed.Value = 0
local data
local success, err = pcall(function()
data = datastore:GetAsync(player.UserId)
end)
if success and data then
wins.Value = data[1]
--Stage.Value = data[2]
TimePlayed.Value = data[2]
else
print("The player has no data")
end
Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
saveDate(player)
end)
if success then
print("data has been saved")
else
print("data has not been saved!")
end
end)
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveDate(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
I still don’t see any difference; you just added the local Players; the PlayerRemoving function is still inside the PlayerAdded function; and just remove the for loop at the very end; it’s unneccessary.
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("Dvddlayer")
local Players = game:GetService("Players")
local function saveDate(player)
local tableToSave = {
player.leaderstats.Wins.Value;
player.TimePlayed.Value
}
datastore:SetAsync(player.UserId, tableToSave)
end
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
wins.Value = 0
local TimePlayed = Instance.new("IntValue")
TimePlayed.Name = "TimePlayed"
TimePlayed.Parent = player
TimePlayed.Value = 0
local success, data = pcall(function()
return datastore:GetAsync(player.UserId)
end)
if success and data then
wins.Value = data[1]
TimePlayed.Value = data[2]
end
end)
Players.PlayerRemoving:Connect(function(player)
pcall(saveDate, player)
end)