Recently I have been looking at a game I made a while back and gave up on. But now that I’m looking at the game I have been testing it for any errors so that I can fix them and continue developing on it.
But I keep getting one error with no errors in the output. The error is, with datastore once the player has left the game it should save what’s in their backpack. The script doesn’t work and the output isn’t showing any errors
The Script:
local datastoreservice = game:GetService("DataStoreService")
local dataStore = datastoreservice:GetDataStore("BackpackSave")
game.Players.PlayerAdded:Connect(function(player)
pcall(function()
local tool = dataStore:GetAsync("User_"..player.UserId)
if tool then
for i,v in pairs(tool) do
local toolFound = game.ReplicatedStorage.Tools:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = player.Backpack
toolFound:Clone().Parent = player.StarterGear
end
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
local toolsave = {}
for i, tool in pairs(player.Backpack:GetChildren()) do
if tool then
table.insert(toolsave,tool.Name)
end
end
dataStore:SetAsync("User_"..player.UserId,toolsave)
end)
end)
Also if it helps I was following this tutorial (Go to 5:41 minutes to see when they begin the backpack tool saving)
One last thing, I have another datastore saving data but it is not overflowing the datastore queue with datastore’s because there is no error. Could this potentially be the problem?
local datastoreservice = game:GetService("DataStoreService")
local dataStore = datastoreservice:GetDataStore("BackpackSave")
game.Players.PlayerAdded:Connect(function(player)
local tool = dataStore:GetAsync("User_"..player.UserId)
if tool then
for i,v in pairs(tool) do
local toolFound = game.ReplicatedStorage.Tools:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = player.Backpack
toolFound:Clone().Parent = player.StarterGear
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local toolsave = {}
for i, tool in pairs(player.Backpack:GetChildren()) do
if tool then
table.insert(toolsave,tool.Name)
end
end
dataStore:SetAsync("User_"..player.UserId,toolsave)
end)
Glad to be of help even though all I mentioned was for you to print it, you sitll need to figure out how to not make it nil, which if I remember correctly, utilises JSONEncode and JSONDecode from the HTTPService if I’m not mistaken. This should work if your data is a table, which looks like it is