Can u tell what wrong with this script?
local data = game:GetService("DataStoreService"):GetDataStore("Inventory") game.Players.PlayerAdded:Connect(function(plr) local SavedItems = data:GetSync(plr.userId) if SavedItems ~= nil then for i, v in pairs(SavedItems)do if game.ServerStorage.Inventory:FindFirstChild(v) ~=nil then local item = game.ServerStorage.Inventory:FindFirstChild(v) :Clone() item.Parent = plr:WaitForChild("Backpack") end end end plr.CharacterRemoving:Connect(function() plr.Character.Humanoid:UnequipTools() end) end) game.Players.PlayerRemoving:Connect(function(plr) local Table = {} for i, v in pairs(plr.Backpack:GetChildren())do table.insert(Table,v.Name) end if Table ~= nil then data:Sync(plr.userId,Table) end end)
If wrong...???? can u comment a data item saver/backpack saver for the player? and can u add... when they die they can't loose there items.
When you read and overwrite data your not typing the correct function names.
Reading data
To read data you need to use GetAsync()
not GetSync()
Example:
local data = game:GetService("DataStoreService"):GetDataStore("Inventory") game.Players.PlayerAdded:Connect(function(plr) local SavedItems = data:GetAsync(plr.UserId) end
Saving data
This is the same problem as reading data. You named the functions incorrectly.
Sync() is not a function of DataStore. Instead of using Sync() use SetAsync()
which will overwrite data.
Example:
game.Players.PlayerRemoving:Connect(function(plr) local Table = {} for i, v in pairs(plr.Backpack:GetChildren()) do table.insert(Table,v.Name) end if Table ~= nil then data:SetAsync(plr.userId,Table) end end)
Here is a re-written version of your script:
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)
Now you might not need to use the pcalls but they sure came in handy to me. NOTE: "THIS SCRIPT IS NOT TESTED. IT COULD THROW ERRORS AND MAY NOT WORK." (Used caps lock not to be mean but to make sure you see that)
I wrote the script above to fix any bugs with your script. It should work just by copying the script but you may have some tweaking to do. Hope this helps!