ChildAdded Not firing

So I am trying to create inventory and whenever item gets added into the inventory folder it should appear on inventory GUI but childAdded is not firing. can anyone tell me why?

local Players = game:GetService("Players")
local InventoryEvent = game.ReplicatedStorage.InventoryEvent

local function playerSetup(player)
	local Inventory = Instance.new("Folder", player);
	Inventory.Name = "Inventory";
	
	Inventory.ChildAdded:Connect(function(item)
		print("sending...");
		InventoryEvent:FireClient(player, item.name, true);
	end)
	
	InventoryEvent.OnServerEvent:Connect(function(player, ItemName, Value)
		if(Value == false) then
			local SelectedItem = player.Inventory:FindFirstChild(ItemName);
			
			SelectedItem.Parent = game.Workspace
			SelectedItem.Model.PrimaryPart = SelectedItem.Model.Primary;
			SelectedItem.Model:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame +Vector3.new(0, 0, 2));
		else
			
		end
	end)
	
	
	
	
	
	
	
	-------------------------------------------
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 5000
	coins.Parent = leaderstats
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = leaderstats
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Value = 0
	Wins.Parent = leaderstats
	
	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills"
	Kills.Value = 0
	Kills.Parent = leaderstats
end

Players.PlayerAdded:Connect(playerSetup)
4 Likes

Are you 100% sure Instances are added in that folder? Try printing something at the top of the playerSetup function to see that it gets called.

1 Like

Yes, it gets added and i printed ok before the playerSetup function

1 Like

Add a task.wait() before using Instance.new(). You are essentially running ChildAdded and Instance.new() at the same time, causing it to not fire.

not working, btw this is code for creating item frame

local InventoryEvent = game.ReplicatedStorage.InventoryEvent
local ItemFrame = script.Parent:FindFirstChild("ItemFrame");

InventoryEvent.OnClientEvent:Connect(function(player, ItemName, Value)
	print("yes\n");
	if (Value == true) then
		local Sample = ItemFrame:FindFirstChild("Sample"):Clone();
		Sample.Visible = true
		Sample.TextLabel.Text = ItemName;
		Sample.Name = ItemName
		Sample.Parent = ItemFrame
	end
end)

Perhaps you are firing the remote before the ChildAdded event is defined?

Parent should be Inventory instead. Thought for some reason the ChildAdded event was for the leaderstats.

No no it doesnt do anything, we are looking at childAdded and client event

It’s not firing because the Parent is workspace and not the designated Inventory folder. If you need the objects visible, then move the folder to workspace.

Is the object available on the server? Doesn’t work if an item is added locally.

When the :FireServer() is called, the Inventory gets a new instance- but the current parent is workspace and not the Inventory folder.

Oooh, I clone and add item in localscript:

purchasebutton.MouseButton1Click:Connect(function()
	if player.leaderstats.Coins.Value >= swords[position]:GetAttribute("Price") then
		player.leaderstats.Coins.Value -= swords[position]:GetAttribute("Price");
		local clone = swords[position]:Clone();
		clone.Parent = player.Inventory;
	end
end)

DescendantAdded will work better than ChildAdded, give that a shot.

1 Like

Nope It Doesnt work, but i tried to add sword via remoteEvent and it gave me this error.

Attempt to set parent of Players.GMAL222 to Players.GMAL222.Inventory would result in circular reference

this is code

purchasebutton.MouseButton1Click:Connect(function()
	if player.leaderstats.Coins.Value >= swords[position]:GetAttribute("Price") then
		player.leaderstats.Coins.Value -= swords[position]:GetAttribute("Price");
		local clone = swords[position]:Clone();
		print(clone);
		game.ReplicatedStorage.AddItem:FireServer(player, clone);
	end
end)
local AddItem = game.ReplicatedStorage.AddItem

AddItem.OnServerEvent:Connect(function(player, sword)
	sword.Parent = player.Inventory
end)

OnServerEvent instantly takes player as the first argument, remove the player object in FireServer. You have to create the sword on the server, the sword would be nil as objects created by the client doesn’t replicate to the server

It gave me “ServerScriptService.AddItem:4: attempt to index nil with ‘Parent’” error

Create the sword on the server, not on the local script

Sword Is Getting Replicated on The Server and Event in Childadded is firing but why isnt it not showing up?

local InventoryEvent = game.ReplicatedStorage.InventoryEvent
local ItemFrame = script.Parent:FindFirstChild("ItemFrame");

InventoryEvent.OnClientEvent:Connect(function(player, ItemName, Value)
	print("yes\n");
	if (Value == true) then
		local Sample = ItemFrame:FindFirstChild("Sample"):Clone();
		Sample.Visible = true
		Sample.TextLabel.Text = ItemName;
		Sample.Name = ItemName
		Sample.Parent = ItemFrame
	end
end)

Change this to item.Name
Again, OnClientEvent does not take player as its first argument, so remove the player from the function (NOT FireClient)
It should work now

1 Like

aww i was about to answer that :[

1 Like