SO here is my code(With error):
------------------------------------ game.Players.PlayerAdded:connect(function(player) local DataStore = game:GetService("DataStoreService"):GetDataStore(player.userId) if player.Name ~= "Player" then player:WaitForDataReady() --Safely Load Data-- local AllTools = game.lighting:GetChildren() local S1 = DataStore:GetAsync("Tools") for i,v in pairs(AllTools) do if v.Name == S1[i.Name] then -- attempt to index I (a Num Value) v:Clone().Parent = player.Backpack end end end end) game.Players.PlayerRemoving:connect(function(player) game.OnClose = function() pcall(function() local DataStore = game:GetService("DataStoreService"):GetDataStore(player.userId) local Tools = player.Backpack:GetChildren() local s1 = {} for i=1,#Tools do if Tools[i].ClassName == "Tool" then local n = i s1[n] = Tools[i].Name end end DataStore:SetAsync("Tools",s1) end) end end) ---------------------------------------
SO the idea is that your tools save with data stores with the names being set as a string, then when they load back into the game it clones the tools from lighting, going through the table, then the tools in lighting. This is a simple sketch I whipped up but how would I get it to load the table back in? (around line 12 where the error is) And would this code necessarily save all tools? because if you have a tool equipped it goes to the player and not the backpack, how would I add that too? All help appreciated, Thx, Bubs
There are a lot of problems with the way you are using the data store in your code, 1st you should not be creating a new data store for every player as you should be changing the key, 2nd you should not be using lighting to sore object and lastly you should not be using the on close event like this. You need to save the data when the player leaves the game not when the server closes.
Example:-
-- we get the data store at the top so we don't need to get it each time a plr is added / removed -- The current setup tries to get a new data store for every player, you should be using one data store and changing the keys local dsServ = game:GetService('DataStoreService'):GetDataStore('Testing') -- Tools should be stored in ServerStorage and not in lighting local toolList = {} for _, tool in pairs(game:GetService('ServerStorage'):GetChildren()) do -- just make this into a key value pair so we dont need to loop to find each tool toolList[tool.Name] = tool -- tools must have different names else it will be overwritten end game.Players.PlayerAdded:connect(function(plr) -- we use a a key to access the player data, this key is made unique with the UserId local key = 'tools_' .. tostring(plr.UserId) local data = dsServ:GetAsync(key) local backPack = plr:WaitForChild('BackPack') if data then -- data found for _, tool in pairs(data) do if toolList[tool.Name] then toolList[tool.Name]:Clone().Parent = backPack end -- add tool if found in the list end else -- no data found end end) game.Players.PlayerRemoving:connect(function(plr) local key = 'tools_' .. tostring(plr.UserId) -- build a list of tools local saveList = {} for _, tool in pairs(plr.BakcPack:GetChildren()) do table.insert(saveList, tool.Name) end -- save tool list dsServ:SetAsync(key,saveList) end)
I hoe this example helps, please comment if you do no understand how / why this code works.
Quote from the wiki:
Keys must be strings. The value is the information you want to store or access. Values can be numbers, booleans, strings, or Lua tables. However, values cannot be ROBLOX types like Vector3, Instance, or Part.
http://wiki.roblox.com/index.php?title=Data_store
Instead you could save the tools names then find them in say ReplicatedStorage then put them in the player's backpack.