How to find the previous parent of an instance?

I’m trying to make an inventory system where if the players has more than 6 tools, it prevents the player from getting more tools. However when a player equips and then un-equips a tool, it increases the value. It needs to make sure the previous parent wasn’t the character so it can increase the value the desired way.

player.Backpack.ChildAdded:Connect(function(tool)
	tool.AncestryChanged:Connect(function(child, parent)
		if child.Name ~= "Tablet" then
		    player.ToolAmount.Value += 1
		end
	end)
end)

Could anyone help me?

1 Like

So what you can do is that you can first check if the child that being added to you backpack is a tool, then using for loop to loop through the Backpack folder to see how many tools you currently have instead of manually counting it yourself. Here is the code that you can start with.


player.Backpack.ChildAdded:Connect(function(child)
       if child:IsA(“Tool”) then
             — loop through the folder.
       end
end)

1 Like

This was what I was going for initially, but it has issues whenever a player a equips a tool on their character and then picks up an item.

1 Like

Probably because you don’t have a ChildRemoved function to decrease the value by 1

1 Like

I do, but I didn’t show it in the code. I don’t want it to decrease whenever a player equips a tool on their character

1 Like

The ChildRemoved code:

player.Backpack.ChildRemoved:Connect(function(tool)
	tool.AncestryChanged:Connect(function(child, parent)
		if child.Parent.Name == "Workspace" and child.Name ~= "Tablet" then
            player.ToolAmount.Value -= 1
		end
	end)
end)
1 Like

That’s why you need a -1 in the ChildRemoved function.
*** EDIT, just saw your previous post.

My issue is that it increases the value when a player equips and unequips a tool.

What if you put an Int NumberValue in the backpack as well and use it to set the number of the items in the backpack.
When you equip or unequip a tool don’t +1 or -1 it from that value, only -1 when an item is dropped or + 1 when an object is picked up. If the number is ‘full’ then don’t allow the player to pick up another item.

Why can’t you just use this?

Character.ChildAdded:Connect(function(Child) -- player equipped tool.
   if Child:IsA("Tool") then
      player.ToolAmount.Value +=1 
   end
end)
1 Like

Can you just check #equipment:GetChildren() instead of tracking the number yourself? Then it will always match the amount of tools in equipment.

(I don’t know entirely where you are tracking so I just called it equipment)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.