First, my code:
local DATASTORESERVICE = game:GetService("DataStoreService") local toolStore = DATASTORESERVICE:GetDataStore("-tools") game.Players.PlayerRemoving:Connect(function(player) pcall(function() local tools = {} for _,v in pairs(player.Backpack:GetChildren()) do table.insert(tools,1,v) print("Saving: "..v.Name) end for _,v in pairs(player.Character:GetChildren()) do if v:IsA("Tool") then table.insert(tools,1,v) print("Saving: "..v.Name) break end end local own = {} for i,v in pairs(game.ServerStorage:GetChildren()) do if tools:FindFirstChild(v.Name) then table.insert(own,i,true) else table.insert(own,i,false) end end toolStore:SetAsync("-tools"..player.UserId,own) end) end) game.Players.PlayerAdded:Connect(function(player) local success,value = pcall(function() local tools = toolStore:GetAsync("-tools"..player.UserId) end) if value ~= nil then local own = value for i,v in pairs(own) do if v == true then local tool = game.ServerStorage:GetChildren()[i]:Clone() tool.Parent = player.Backpack end end end end)
This is all 1 script. I am trying to store which weapons a user leaves the game with, so that when they re-enter the game, they have those same weapons.
Currently, it seems to save just fine. I have tried saving a string, and it ends up returning an empty table when I use GetAsync.
Why does this not work, or what would I need to do to make this work?
If you have any further questions, please ask them. I really need to get this fixed.
As deth836231 said, the character is not in the game anymore.
Instead make it so when people buy/get the item, make a table where it's true that they own the thing:
local DATASTORESERVICE = game:GetService("DataStoreService") local toolStore = DATASTORESERVICE:GetDataStore("-tools") -- Change to where ever the remote is local gotToolRemote = game.ReplicatedStorage.GotTool local TOOL_TABLE = { ["Random tool"] = false; -- etc. } -- When player buys/get's a tool: gotToolRemote.OnServerEvent:Connect(function(player, tool) -- This fires with the 'tool' varible as the tool in the players backpack -- Now we save the tool: local success, value = pcall(function() local toolsOwned = toolStore:GetAsync(player.UserId) end) if value ~= nil then toolsOwned[tool.Name] = true toolStore:SetAsync(player.UserId, toolsOwned) end end) -- Player added: game.Players.PlayerAdded:Connect(function() local success,value = pcall(function() local tools = toolStore:GetAsync("-tools"..player.UserId) end) if value ~= nil then local own = value for i,v in pairs(own) do if v == true then local tool = game.ServerStorage:GetChildren()[i]:Clone() tool.Parent = player.Backpack end end end end)