Hi guys.Can someone help me with my inventory script?

Hi guys.Some days i was trying to make a inventory to my project. It was working perfectly. but when i reseted the script just broke.

i tried to add CharacterAdded but it didnt fixed the problem.

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		
		
		local PlayerGui = plr:WaitForChild("PlayerGui")
		local Inv = PlayerGui:WaitForChild("Inventory")
		local ServerInventory = game.ServerScriptService.Inventorys[plr.Name.."Inventory"]
		local ClientInventory = PlayerGui.Inventory.Invbase:WaitForChild("BackColor")
				
		local equip = PlayerGui.Inventory.Invbase.Equip.Equip
		local unequip = PlayerGui.Inventory.Invbase.Equip.Unequip
		local Backpack = plr:WaitForChild("Backpack")
		
		
		ServerInventory.ChildAdded:Connect(function(Child)
			
			if Child:IsA("Tool") then
				local Id = Child:FindFirstChild("iconid")
				local ImageButton = Instance.new("ImageButton",ClientInventory)
				selected = Instance.new("BoolValue",ImageButton)
				selected.Name = "Selected"
				selected.Value = false
				ImageButton.Name = Child.Name
				ImageButton.Image = Id.Value		
				
					ImageButton.MouseButton1Click:Connect(function()
					selected.Value = true
					
					equip.MouseButton1Click:Connect(function()
						if selected.Value == true then
							Child.Parent = Backpack
							ImageButton:Destroy()
						end --If selected == true
					end)--Equip pressed
					
						unequip.MouseButton1Click:Connect(function()
							local GetAllBackPackTools = Backpack:GetDescendants()
							for index, GetAllBackPackTools in pairs(GetAllBackPackTools) do
								if GetAllBackPackTools:IsA("Tool") then
									GetAllBackPackTools.Parent = ServerInventory
								end-- 	if GetAllBackPackTools:IsA("Tool") then
							end ---Loop for iv
						end) --Unequip pressed
						
						
					
					
					end)--ImageButtonPressed
				end -- if Child:IsA("Tool")	
			end)--ChildAdded	
		end) --CharacterAdded
	end) --PlayerAdded
1 Like

Any error we should be aware of?

Also, try like this:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		
		local PlayerGui = plr:WaitForChild("PlayerGui")
		local Inv = PlayerGui:WaitForChild("Inventory")
		local ServerInventory = game.ServerScriptService.Inventorys[plr.Name.."Inventory"]
		local ClientInventory = PlayerGui.Inventory.Invbase:WaitForChild("BackColor")
				
		local equip = PlayerGui.Inventory.Invbase.Equip.Equip
		local unequip = PlayerGui.Inventory.Invbase.Equip.Unequip
		local Backpack = plr:WaitForChild("Backpack")
		
		local ChildAddedFunc = nil
		ChildAddedFunc  = ServerInventory.ChildAdded:Connect(function(Child)
			
			if Child:IsA("Tool") then
				local Id = Child:FindFirstChild("iconid")
				local ImageButton = Instance.new("ImageButton",ClientInventory)
				selected = Instance.new("BoolValue",ImageButton)
				selected.Name = "Selected"
				selected.Value = false
				ImageButton.Name = Child.Name
				ImageButton.Image = Id.Value		
				
					ImageButton.MouseButton1Click:Connect(function()
					selected.Value = true
					
					equip.MouseButton1Click:Connect(function()
						if selected.Value == true then
							Child.Parent = Backpack
							ImageButton:Destroy()
						end --If selected == true
					end)--Equip pressed
					
						unequip.MouseButton1Click:Connect(function()
							local GetAllBackPackTools = Backpack:GetDescendants()
							for index, GetAllBackPackTools in pairs(GetAllBackPackTools) do
								if GetAllBackPackTools:IsA("Tool") then
									GetAllBackPackTools.Parent = ServerInventory
								end-- 	if GetAllBackPackTools:IsA("Tool") then
							end ---Loop for iv
						end) --Unequip pressed
						
						
					
					
					end)--ImageButtonPressed
				end -- if Child:IsA("Tool")	
			end)--ChildAdded	
            char:WaitForChild("Humanoid").Died:Connect(function()
               ChildAddedFunc:Disconnect()
            end)
		end) --CharacterAdded
	end) --PlayerAdded

Does your GUI reset on respawn?

No.I disabled the reset on spawn.

1 Like

it fixed the copy. but still dont giving the gear.

1 Like

Have you tried debugging with breakpoints of prints?

1 Like

Ah yes, cause you aren’t checking for any tool that is already existing in the player’s inventory. You’re just checking whenever something gets added in the inventory, not if something already exists.

how do i do this?

like for iv loop? or other way…

1 Like

What I do usually, is using a for loop, and to create a table that contains any found element. Then, use ChildAdded, but if the child is already in the table then ignore it.

2 Likes

oo i’ll try something like this.TY

2 Likes

No problem, let me know if you don’t understand how to do it.

2 Likes