How can I save my Backpack?

2020-11-12 (2)

then try this:


local data_store = game:GetService("DataStoreService")

local elements_store = data_store:GetDataStore("elements")

local players = game:GetService("Players")

local elements_foler = game:GetService("ReplicatedStorage").Elements -- you should have a folder with all the items inside

players.PlayerAdded:Connect(function(player)
	
	local element
	
	local backpack = player.Backpack
	
	local key = "Player_"..player.UserId
	
	local success , element = pcall(function()
	
		return elements_store:GetAsync(key)
		
	end)
	
	if success then
		
		element = elements_foler:FindFirstChild(element):Clone()
		
		element.Parent = backpack
		
	else
		
		elements_store:SetAsync(key,backpack:GetChildren()[1].Name) -- the player may not have chosen the item yet, so it may give an error
		
	end
	
end)

is there an error ?, what’s the problem?

It says Argument 1 missing or nil

May I ask, is your elements data is in ReplicatedStorage?

Yes my element data is in replicatedstorage

I mean the data of the elements. Is it in ReplicatedStorage?

What do you mean by the data of the element?

I mean where is the name of the element data located.

Its inside the folder Elements let me show you a video2020-11-12 (2)

Maybe this will work:

local DataStoreService = game:GetService("DataStoreService")
local ElementStore = DataStoreService:GetDataStore("Elements")
local Players = game:GetService("Players")
local ElementFolder = game:GetService("ReplicatedStorage"):WaitForChild("Elements")

Players.PlayerAdded:Connect(function(player)
	local Backpack = player:WaitForChild("Backpack")
	local Key = "Elements-" .. player.UserId
	local data = nil
	local tries = 0
	local success, err
	
	repeat
		success, err = pcall(function()
			data = ElementStore:GetAsync(Key)
		end)
		
		tries = tries +1
		wait()
	until tries >= 3 or success
	
	if success then
		if data then
			for i, Elements in pairs(data) do
				if ElementFolder:FindFirstChild(data) then
					local CloneElements = ElementFolder:FindFirstChild(data):Clone()
					CloneElements.Parent = Backpack
				end
			end
			print("Data has been successfully loaded for " .. player.Name)
		else
			print(player.Name .. " is a new player.")
		end
	else
		player:Kick("You have been kick to save data")
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local Key = "Elements-" .. player.UserId
	local ElementsTable = {}
	local tries = 0
	local success, err
	
	for i, Elements in pairs(ElementFolder:GetChildren()) do
		table.insert(ElementsTable,Elements.Name)
	end
	
	repeat
		success, err = pcall(function()
			ElementStore:SetAsync(Key,ElementsTable)
		end)
		
		tries = tries +1
		wait()
	until tries >= 3 or success
	
	if success then
		print("Data has been successfully saved for ".. player.Name)
	else
		print("data has not been successfully saved for " .. player.Name)
	end
end)

game:BindToClose(function(player)
	local Key = "Elements-" .. player.UserId
	local ElementsTable = {}
	local tries = 0
	local success, err

	for i, Elements in pairs(player:WaitForChild("Backpack")) do
		table.insert(ElementsTable,Elements.Name)
	end

	repeat
		success, err = pcall(function()
			ElementStore:SetAsync(Key,ElementsTable)
		end)

		tries = tries +1
		wait()
	until tries >= 3 or success

	if success then
		print("Data has been successfully saved for ".. player.Name)
	else
		print("data has not been successfully saved for " .. player.Name)
	end
end)

Okey I’m going to check right now

Can you add my roblox so you can see for your self what im trying to do ?? Oh and it didnt work.

Didnt work i meant to say it didnt

I added you in Roblox @MON_STER0.

Ok i just accepted join me real quick

Can you do it again u just found Drafts

Can you do it again u just found Drafts.

Click it then you will see your script. Click on your script then press commit.