How to datastore for player's Backpack

I tried this script,

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("SaveToolScript")

game.Players.PlayerAdded:Connect(function(plr)
    pcall(function()
        local tool = DataStore:GetAsync("User-"..plr.UserId)
        if tool then
            for i,v in pairs(tool) do
                local ToolFound = game.ReplicatedStorage.BuyToolItems:FindFirstChild(v)
                if ToolFound then
                    ToolFound:Clone().Parent = plr.Backpack
                    ToolFound:Clone().Parent = plr.StarterGear
                end
            end
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    pcall(function()
        local toolsave = {}
        for i, tool in pairs(plr.Backpack:GetChildren()) do
            if tool then
                table.insert(toolsave, tool.Name)
            end
        end
        DataStore:SetAsync("User-"..plr.UserId, toolsave)
    end)
end)

but it didn’t work
How do I fix this?

Can’t you just save the names of the tools then search for them once the player is loaded in?

can u put it in a script how I should fix it

Wait nevermind, I thought you were trying to save instances and not the names

Did you turn on “Enable Stuido Access to API Services” in roblox stuido game settings?

You can follow these steps/

It will tell you everything you need here!

Don’t forget to solve if that fixed your problem.

Everything is already turned on.

Did you check the Output? first.

Did it give you a error message or warn. before we all do that

no error popped up in the output

Remove the pcall() and check for errors again. I think its probably because you can’t save tables(or can?).

this is my script now and it still doesnt work.

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("SaveToolScript")

game.Players.PlayerAdded:Connect(function(plr)
        local tool = DataStore:GetAsync("User-"..plr.UserId)
        if tool then
            for i,v in pairs(tool) do
                local ToolFound = game.ReplicatedStorage.BuyToolItems:FindFirstChild(v)
                if ToolFound then
                    ToolFound:Clone().Parent = plr.Backpack
                    ToolFound:Clone().Parent = plr.StarterGear
                end
            end
        end
    end)

game.Players.PlayerRemoving:Connect(function(plr)
        local toolsave = {}
        for i, tool in pairs(plr.Backpack:GetChildren()) do
            if tool then
                table.insert(toolsave, tool.Name)
            end
        end
        DataStore:SetAsync("User-"..plr.UserId, toolsave)
    end)

Look at the output. Are you getting errors?

I’m still not getting any errors

You can try this: https://youtu.be/E082PFJJHOM

Just a warning, PlayerRemoving doesn’t always fire in studio. For studio testing it’s recommended to use BindToClose() instead.

https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("SaveToolScript")
local players = game:GetService("Players")
local storage = game:GetService("ReplicatedStorage")
local buyToolItems = storage:WaitForChild("BuyToolItems")

players.PlayerAdded:Connect(function(plr)
	local res = DataStore:GetAsync("User-"..plr.UserId)
	if res then
		for i,v in pairs(res) do
			local storageTool = buyToolItems:FindFirstChild(v)
			if storageTool then
				local toolBackpack = storageTool:Clone()
				toolBackpack.Parent = plr:WaitForChild("Backpack")
				local toolStarter = storageTool:Clone()
				toolStarter.Parent = plr:WaitForChild("StarterGear")
			end
		end
	end
end)

players.PlayerRemoving:Connect(function(plr)
	local backpackTools = {}
	local backpack = plr:WaitForChild("Backpack")
	for i, tool in pairs(backpack:GetChildren()) do
		if tool then
			table.insert(backpackTools, tool.Name)
		end
	end
	DataStore:SetAsync("User-"..plr.UserId, backpackTools)
end)

game:BindToClose(function()
	local playerList = players:GetPlayers()
	for i, plr in pairs(players) do
		local backpackTools = {}
		local backpack = plr:WaitForChild("Backpack")
		for i, tool in pairs(backpack:GetChildren()) do
			if tool then
				table.insert(backpackTools, tool.Name)
			end
		end
		DataStore:SetAsync("User-"..plr.UserId, backpackTools)
	end
end)
1 Like