here's the code:
local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("Inventory") -- Separate variable local function Load(plr) local key = "plr-"..plr.UserId -- key variable (The way I do it) local savedData -- Will be set below local success, err = pcall(function() -- This makes sure that the script will keep going if it errors savedData = DataStore:GetAsync(key) -- GetAsync() end -- if datastore failed to read data if not success then warn("Failed to read data"..tostring(err)) return -- this will return nil end -- if datastore succeeded if savedData then -- if the player has data then load the data for i, v in pairs(savedData) do if v then local item = game.ServerStorage.Inventory:FindFirstChild(v):Clone() if item then item.Parent = plr:WaitForChild("Backpack") end end end else -- If the player does not have data to be loaded local items = {} local itemsSaving = plr:WaitForChild("Backpack"):GetChildren() for i, v in pairs(itemsSaving) do if v then table.insert(items, v.Name) end end local success, err = pcall(function() -- protected call DataStore:SetAsync(key, items) -- SetAsync() end -- if overwriting data failed if not success then warn("Failed to overwrite data"..tostring(err)) return end end end local function SaveData(plr) local key = "plr-"..plr.UserId -- Again just how I do it local items = {} local itemsSaving = plr:WaitForChild("Backpack"):GetChildren() for i, v in pairs(itemsSaving) do if v then table.insert(items, v.Name) end end local success, err = pcall(function() DataStore:SetAsync(key, items) -- SetAsync() end -- failed if not success then warn("Failed to overwrite data"..tostring(err)) return end end -- Calls game.Players.PlayerAdded:Connect(Load) game.Players.PlayerRemoving:Connect(SaveData)
Can u test it guys? how doi fix this or rescript it?
It is NOT possible to save objects to a DataStore. Instead, you could probably save the names of the tools, and when the player comes back, it would give them tools according to the names in their saved data.