Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Backpack inventory gui empty after resetting or dying?

Whenever a player loads into the server their inventory is saved, meaning all items inside are placed into player.backpack.

If a player dies or reset all the items dissapear from the gui, they are still inside the player.backpack but the gui doesnt show them anymore.

the DataStoreScript i use:

local rs = game:GetService("ReplicatedStorage")
local ds = game:GetService("DataStoreService")
local store = ds:GetDataStore("saveStore")
local library = rs:WaitForChild("Library")

--< directory functions

local dir = {}

local function edit(player, list)
	dir[player.Name] = list
end

local function setup(player, list)
	for i = 1, #list do
		local tool = library:FindFirstChild(list[i])
		if tool then
			local clone = tool:Clone()
			clone.Parent = player.Backpack
		else
			print(list[i] .. " not found")
		end
	end
end

--< player events

game.Players.PlayerAdded:connect(function(player)

local ready = false

	player.CharacterAdded:connect(function(char)

	local bp = player.Backpack
	local data = nil
	
	if ready == false then
		ready = true
		
		data = store:GetAsync(player.UserId)
		
		if data then
			setup(player, data)
			edit(player, data)
		end
	end	
	
	char.Humanoid.Died:connect(function()
		char.Humanoid:UnequipTools()
		
		local old = player.StarterGear:GetChildren()
		for i = 1, #old do
			old[i]:Destroy()
		end
		
		local new = player.Backpack:GetChildren()
		for i = 1, #new do
			new[i].Parent = player.StarterGear
		end		
	end)	
	
	--< adjuster
	
	local count = 0
	
	local function adjust()
	if char.Humanoid.Health > 0 then
	
		local list = {}
	
		local equipped = char:FindFirstChildOfClass("Tool")
		if equipped then
			table.insert(list, equipped.Name)
		end	
	
		local tools = bp:GetChildren()
		for i = 1, #tools do
			table.insert(list, tools[i].Name)
		end
	
		if count ~= #list then
			edit(player, list)
			count = #list
		end
	end
	end

	
	--< child events
	
	bp.ChildAdded:connect(adjust)	
	bp.ChildRemoved:connect(adjust)	
	
	char.ChildAdded:connect(function(child)
		if child.ClassName == "Tool" then
			adjust()
		end
	end)
	
	char.ChildRemoved:connect(function(child)
		if child.ClassName == "Tool" then
			adjust()
		end
	end)	
end)

end)

game.Players.PlayerRemoving:connect(function(player)
	store:SetAsync(player.UserId, dir[player.Name])
	dir[player.Name] = nil
end)

--< safety

game:BindToClose(function()
	wait(5)
end)

---BackPackTEST----

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("BackpackSave")

game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local tool = dataStore:GetAsync("User-"..player.UserId)
		if tool then
			for i,v in pairs(tool)do
				local toolFound = game.ReplicatedStorage.Items:FindFirstChild(v)
				if toolFound then
					toolFound:clone().Parent = player.Backpack
					toolFound:clone().Parent = player.StarterGear
				end
			end
		end
	end)
end)

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

The local script inside the frame for the backpack gui


local player = game.Players.LocalPlayer
local character = player.Character
local items = {}
local buttons = {}
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false) -- Makes the original backpack gui invisible

function search(location)
	for i,v in pairs(location:GetChildren()) do -- Find all item in a specific location
		if v:isA("Tool") then -- If the item found is a "Tool"
			table.insert(items,v) -- We're going to put all the tools found in a table.
		end
	end
end

function refresh()
	for i,v in pairs(buttons) do -- Finds all items in the table
		v:Destroy() -- Destroy 'em all
	end
	for i,v in pairs(items) do -- Finds all items in the table
		local button = script.Sample:Clone() -- clones the sample button inside the localscript
		button.Name = v.Name -- sets the cloned button's name to the name of the item
		button.LayoutOrder = i
		button.Parent = script.Parent.Handler -- Sets the parent of the cloned button to the handler
		button.Image = v.TextureId -- Sets the image of the button to the texture id of the tool
		table.insert(buttons,button) -- Inserts the button to our table "buttons"
		button.MouseButton1Click:connect(function()
			if script.Parent.Handler.Selected.Value == nil or script.Parent.Handler.Selected.Value ~= v then -- Checks if the selected value is nothing or if the selected value is not the button
				script.Parent.Frame.ItemName.Text = v.Name -- Sets the TextLabel's Text to the name of the tool/button
				script.Parent.Frame.ImageLabel.Image = v.TextureId -- Sets the image label's image to the texture id of the tool
				script.Parent.Handler.Selected.Value = v
				if script.Parent.Handler.Selected.Value ~= script.Parent.Handler.Equipped.Value then --if the selected value is not the same as the equipped value then
					script.Parent.Handler.Location.Value = v.Parent -- Sets the value of our location to the parent of the tool whether it is in the backpack or in the character
					script.Parent.Frame.Equip.Text = "Equip" -- Self explanatory
				elseif script.Parent.Handler.Selected.Value == script.Parent.Handler.Equipped.Value then -- If the selected value is the same as the equipped value then...
					script.Parent.Handler.Location.Value = v.Parent
					script.Parent.Frame.Equip.Text = "Unequip"
				end
			end
		end)
	end
end

function backpackRefresh()
	items = {}
	search(character)
	search(player.Backpack)
	refresh()
end

backpackRefresh()

player.Backpack.ChildAdded:connect(backpackRefresh)
player.Backpack.ChildRemoved:connect(backpackRefresh)

character.ChildAdded:connect(backpackRefresh)
character.ChildRemoved:connect(backpackRefresh)

4 comments

0

def not a free model ANYWAYS i feel like line 9 of the local script should have v:IsA instead of v:isA. I might be wrong though

0

if that was not the problem then you mind showing any dev console errors?

0

There are no errors, the problem is within the datastore script. If its disabled the backpack works fine but doesnt save between joining and leaving the place as thats where i need the datasave for. i thought something with this function or line was causing the problem

0

nvm fixed it

1 answer

048	    char.Humanoid.Died:connect(function()
049	        char.Humanoid:UnequipTools()
050	 
051	        local old = player.StarterGear:GetChildren()
052	        for i = 1, #old do
053	            old[i]:Destroy()
054	        end
055	 
056	        local new = player.Backpack:GetChildren()
057	        for i = 1, #new do
058	            new[i].Parent = player.Backpack
059	        end    
060	    end)   

had to change player.StarterGear into player.Backpack or just bp

This discussion is preserved as it was. New answers, comments, and votes are closed.