Child added event not running

I’m making custom hotbar system and to sync the folder data and the backpack I create event and function but event not firing. Pleas ehlep

-- Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

--------------------------------------------------
local function GetValidPosition(Folder, Haystack, mockItem)
	-- inserts values into table
	for i, v in pairs(Folder:GetChildren()) do
		table.insert(Haystack, v.Value)
	end
	-- checks if the value is in the table and if not it choose random value
	local Found = false
	repeat
		if not table.find(Haystack, mockItem.Value) then
			mockItem.Parent = Folder
			Found = true
		else
			mockItem.Value = mockItem.Value + 1
		end
	until Found
end

Players.PlayerAdded:Connect(function(player)
	print(player)
	-- sets up energy bar on spawn like health
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:WaitForChild("Humanoid")
		local Energy = Instance.new("NumberValue")
		Energy.Name = "Energy"
		Energy.Value = 100 -- replace with Player.PlayerData.Energy.Max when add that and a bindable event wait()
		Energy.Parent = Humanoid
	end)
	-- variables
	local PlayerFolder = ServerStorage.Players:WaitForChild(player.Name)
	local PlayerInventory = PlayerFolder:FindFirstChild("BackpackFolder")
	local Hotbar = PlayerInventory.Hotbar
	local BackpackFolder = PlayerInventory.Backpack
	print(player.Backpack)
	-- adds mimic item to the mimic player (creates a intvalue "copy" of the item and puts in a folder holding player stats)
	player.Backpack.ChildAdded:Connect(function(item)
		print(item, "hello")
		if not Hotbar:FindFirstChild(item.Name) and not BackpackFolder:FindFirstChild(item.Name) then
			print("ran")
			local mockItem = Instance.new("IntValue")
			mockItem.Name = item.Name
			mockItem.Value = 1
			local Haystack = {}
			-- checks if there is space to put in these folders
			if #Hotbar:GetChildren() < 10 then
				GetValidPosition(Hotbar, Haystack, mockItem)
			elseif #BackpackFolder:GetChildren() < 100 then
				GetValidPosition(BackpackFolder, Haystack, mockItem)
			else
				print("Way too many items lol")
			end
		end
	end)
end)

Edit: for some reason this code doesn’t work the first time i run the game after opening studio but it does every other time after

-- Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

--------------------------------------------------
local function GetValidPosition(Folder, Haystack, mockItem)
	-- inserts values into table
	for i, v in pairs(Folder:GetChildren()) do
		table.insert(Haystack, v.Value)
	end
	-- checks if the value is in the table and if not it choose random value
	local Found = false
	repeat
		if not table.find(Haystack, mockItem.Value) then
			mockItem.Parent = Folder
			Found = true
		else
			mockItem.Value = mockItem.Value + 1
		end
	until Found
end

Players.PlayerAdded:Connect(function(player)
	print(player)
	-- sets up energy bar on spawn like health
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:WaitForChild("Humanoid")
		local Energy = Instance.new("NumberValue")
		Energy.Name = "Energy"
		Energy.Value = 100 -- replace with Player.PlayerData.Energy.Max when add that and a bindable event wait()
		Energy.Parent = Humanoid
	end)
	-- variables
	local PlayerFolder = ServerStorage.Players:WaitForChild(player.Name)
	local PlayerInventory = PlayerFolder:FindFirstChild("BackpackFolder")
	local Hotbar = PlayerInventory.Hotbar
	local BackpackFolder = PlayerInventory.Backpack
	print(player.Backpack)
	-- adds mimic item to the mimic player (creates a intvalue "copy" of the item and puts in a folder holding player stats)
	player.ChildAdded:Connect(function(item)
		if item.Name == "Backpack" then
			player.Backpack.ChildAdded:Connect(function(item)
				print(item, "hello")
				if not Hotbar:FindFirstChild(item.Name) and not BackpackFolder:FindFirstChild(item.Name) then
					print("ran")
					local mockItem = Instance.new("IntValue")
					mockItem.Name = item.Name
					mockItem.Value = 1
					local Haystack = {}
					-- checks if there is space to put in these folders
					if #Hotbar:GetChildren() < 10 then
						GetValidPosition(Hotbar, Haystack, mockItem)
					elseif #BackpackFolder:GetChildren() < 100 then
						GetValidPosition(BackpackFolder, Haystack, mockItem)
					else
						print("Way too many items lol")
					end
				end
			end)
		end
	end)
end)

oh my god wrong topic im so tired

-- Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

--------------------------------------------------
local function GetValidPosition(Folder, Haystack, mockItem)
	-- inserts values into table
	for i, v in pairs(Folder:GetChildren()) do
		table.insert(Haystack, v.Value)
	end
	-- checks if the value is in the table and if not it choose random value
	local Found = false
	repeat
		if not table.find(Haystack, mockItem.Value) then
			mockItem.Parent = Folder
			Found = true
		else
			mockItem.Value = mockItem.Value + 1
		end
	until Found
end

Players.PlayerAdded:Connect(function(player)
	print(player)
	-- sets up energy bar on spawn like health
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:WaitForChild("Humanoid")
		local Energy = Instance.new("NumberValue")
		Energy.Name = "Energy"
		Energy.Value = 100 -- replace with Player.PlayerData.Energy.Max when add that and a bindable event wait()
		Energy.Parent = Humanoid
	end)
	-- variables
	local PlayerFolder = ServerStorage.Players:WaitForChild(player.Name)
	local PlayerInventory = PlayerFolder:FindFirstChild("BackpackFolder")
	local Hotbar = PlayerInventory.Hotbar
	local BackpackFolder = PlayerInventory.Backpack
	print(player.Backpack)
	-- adds mimic item to the mimic player (creates a intvalue "copy" of the item and puts in a folder holding player stats)
	player.Backpack.ChildAdded:Connect(function(item)
		print(item, "hello")
		if not Hotbar:FindFirstChild(item.Name) and not BackpackFolder:FindFirstChild(item.Name) then
			print("ran")
			local mockItem = Instance.new("IntValue")
			mockItem.Name = item.Name
			mockItem.Value = 1
			local Haystack = {}
			-- checks if there is space to put in these folders
			if #Hotbar:GetChildren() < 10 then
				GetValidPosition(Hotbar, Haystack, mockItem)
			elseif #BackpackFolder:GetChildren() < 100 then
				GetValidPosition(BackpackFolder, Haystack, mockItem)
			else
				print("Way too many items lol")
			end
		end
	end)
	player.Backpack.ChildAdded:Connect(function()
		print("child added")
	end)
end)


this code work for some reason i too tired to figure out

1 Like

now it dont work idek what going on

it fix or i changed idk remeber bro :sob:

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