How to detect if 2 children added at same time

So, Today I’ve wanted to make my custom inventory


and this is how faar I’ve got, everything seems fine until 2 children get added at the same time
grafik
Now 2 items are in the same bar, adding wait or something doesnt fix it, ive tried and look around enough
grafik
This is what happends if i start dragging it.
grafik

Dragging it to another place fixes it, but its still of a big issue.

Script:

script.Parent.Visible = true
local isdragging = nil
local UIS = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
local nums = {
	One = 1,
	Two = 2,
	Three = 3,
	Four = 4,
	Five = 5,
	Six = 6,
	Seven = 7,
	Eight = 8,
	Nine = 9,
	Zero = 0,
	Dash = "-",
	Dash1 = "-",
	Dash2 = "-"
}

local db = false

for i = 0,9 do
	local ItemTag = script.Parent.BG.UIListLayout.ToolItem:Clone()
	ItemTag.Parent = script.Parent.BG
	if i <= 0 then
		ItemTag.LayoutOrder = 10
	else
		ItemTag.LayoutOrder = i
	end
end
for i = 1,3 do
	local ItemTag = script.Parent.BG.UIListLayout.ToolItem:Clone()
	ItemTag.Parent = script.Parent.BG
	ItemTag.LayoutOrder = 11
end
local inv = game.Players.LocalPlayer.Backpack

local function createInv(places,child)
	--game:GetService("RunService").RenderStepped:Wait()
	wait()
	if child then
		if child.Parent == game.Players.LocalPlayer.Backpack then
			local toolbox = script.Parent.BG.UIListLayout.Tool:Clone()
			toolbox.Parent = places
			toolbox.BG.Item.Value = child
			toolbox.BG.ItemName.Text = child.Name
			toolbox.BG.ItemDesc.Text = child.ToolTip
			toolbox.BG.ItemImage.Image = child.TextureId

			if child:FindFirstChild("Configuration") then
				if child.Configuration:FindFirstChild("HasPure") then
					if child.Configuration:FindFirstChild("HasPure").Value then
						if child.Configuration:FindFirstChild("HasPure").Value:FindFirstChild("Configuration") then
							if child.Configuration:FindFirstChild("HasPure").Value:FindFirstChild("Configuration"):FindFirstChild("Image") then
								local effect = child.Configuration:FindFirstChild("HasPure").Value:FindFirstChild("Configuration"):FindFirstChild("Image")
								toolbox.BG.ItemEffect.Image = effect.Image
								toolbox.BG.ItemEffect.ImageColor3 = effect.ImageColor3
								toolbox.BG.ItemEffect.ImageTransparency = effect.ImageTransparency
								toolbox.BG.ItemEffect.ScaleType = effect.ScaleType
							end
						end
					end
				end
				if child.Configuration:FindFirstChild("ItemType") then
					if game.ReplicatedStorage.Configuration:FindFirstChild(child.Configuration.ItemType.Value) then
						toolbox.UIGradient.Color = game.ReplicatedStorage.Configuration:FindFirstChild(child.Configuration.ItemType.Value).ToolGradient.Color
						if child.Configuration:FindFirstChild("Amount") then
							toolbox.BG.ItemStack.Visible = true
							toolbox.BG.ItemStack.Text = tostring(child.Configuration:FindFirstChild("Amount").Value)
							child.Configuration:FindFirstChild("Amount").Changed:Connect(function()
								toolbox.BG.ItemStack.Text = tostring(child.Configuration:FindFirstChild("Amount").Value)
							end)
						end
					end
				end
			end

			if places.LayoutOrder == 10 then
				toolbox.BG.ItemOrder.Text = "0"
				toolbox.BG.Number.Value = 0
			elseif places.LayoutOrder <= 9 then
				toolbox.BG.ItemOrder.Text = places.LayoutOrder
				toolbox.BG.Number.Value = places.LayoutOrder
			else
				toolbox.BG.ItemOrder.Text = "-"
				toolbox.BG.Number.Value = -1
			end
			local lastclick = -9999
			local holding = false
			mouse.Move:Connect(function()
				if lastclick+0.1 <= time() and holding == true then
					isdragging = places
					toolbox.Position = UDim2.new(0,mouse.X-places.AbsolutePosition.X,0,mouse.Y-places.AbsolutePosition.Y)
				end
			end)
			toolbox.MouseEnter:Connect(function()
				toolbox.BG.ItemDesc.Visible = true
				toolbox.BG.ItemDesc.Text = child.ToolTip
			end)
			toolbox.MouseLeave:Connect(function()
				toolbox.BG.ItemDesc.Visible = false
			end)
			UIS.InputEnded:Connect(function(input, gameProcessed)
				if input.UserInputType == Enum.UserInputType.MouseButton1 then
					if holding == true and isdragging then
						holding = false
						toolbox.Position = UDim2.new(0.5,0,0.5,0)
						local MousePos =  UIS:GetMouseLocation() - game:GetService("GuiService"):GetGuiInset()
						local selected = game.Players.LocalPlayer.PlayerGui:GetGuiObjectsAtPosition(MousePos.X,MousePos.Y)
						for _,i in pairs(selected) do
							if i.Name == "ToolItem" and isdragging:FindFirstChild("Tool") then
								if i:FindFirstChild("Tool") then
									local replace = i:FindFirstChild("Tool")
									local oldparent = replace.Parent
									local newparent = isdragging.Tool.Parent
									--replace.Parent = newparent
									--isdragging.Tool.Parent = oldparent
									--toolbox = replace
									--	game:GetService("Debris")
									local dragitem = isdragging.Tool.BG.Item.Value
									local repitem = replace.BG.Item.Value
									replace:Destroy()
									isdragging.Tool:Destroy()
									if child.Parent == game.Players.LocalPlayer.Character then
										game.ReplicatedStorage.Files.RemoteEvents.Tool.UnequipTool:FireServer(child)
									end
									createInv(oldparent,dragitem)
									createInv(isdragging,repitem)
								else
									if isdragging:FindFirstChild("Tool") then
										local oldparent = i
										local dragitem = isdragging.Tool.BG.Item.Value
										isdragging.Tool:Destroy()
										if child.Parent == game.Players.LocalPlayer.Character then
											game.ReplicatedStorage.Files.RemoteEvents.Tool.UnequipTool:FireServer(child)
										end
										createInv(oldparent,dragitem)
									end
								end
							end
						end
						if isdragging:FindFirstChild("Tool") and toolbox then
							isdragging.Tool.Position = UDim2.new(0.5,0,0.5,0)
							toolbox.Position = UDim2.new(0.5,0,0.5,0)		
							isdragging = nil
						end
					end
				end
			end)
			places.MouseButton1Down:Connect(function()
				if places:FindFirstChild("Tool") then
					if db == false then
						db = true
						holding = true
						lastclick = time()
						wait()
						db = false
					end
				end
			end)
			places.MouseButton1Up:Connect(function()
				holding = false
				toolbox.Position = UDim2.new(0.5,0,0.5,0)
				if lastclick+0.1 >= time() then
					if places:FindFirstChild("Tool") then
						if db == false then
							db = true
							if child == game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool") then
								game.ReplicatedStorage.Files.RemoteEvents.Tool.UnequipTool:FireServer(game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool"))
							else
								if game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool") then
									local unequip = game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool")
									game.ReplicatedStorage.Files.RemoteEvents.Tool.UnequipTool:FireServer(unequip)
								end
								game.ReplicatedStorage.Files.RemoteEvents.Tool.EquipTool:FireServer(child)
							end	
							wait()
							db = false
						end
					end
				end
			end)
		end
	end
end
local function Add(child)
	if child:IsA("Tool") then
		local find = nil
		for _,i in pairs(script.Parent.BG:GetChildren()) do
			if i:IsA("TextButton") then
				if i:FindFirstChild("Tool") then
					if i.Tool.BG.Item.Value == child then
						find = i
					end
				end
			end
		end
		if find then
			find.Tool.BG.ItemOrder.TextColor3 = Color3.fromRGB(255, 255, 255)
		else
			local places = {}
			for _,i in pairs(script.Parent.BG:GetChildren()) do
				if i:IsA("TextButton") then
					if not i:FindFirstChild("Tool") then
						table.insert(places,i)
					end
				end
			end
			table.sort(places, function(a, b)
				return a.LayoutOrder < b.LayoutOrder
			end)
			if places[1] then
				wait()
				createInv(places[1],child)
			end
		end
	end
end

inv.ChildAdded:Connect(function(child)
	Add(child)
end)

inv.ChildRemoved:Connect(function(child)
	if child.Parent ~= game.Players.LocalPlayer.Character then
		for _,i in pairs(script.Parent.BG:GetChildren()) do
			if i:IsA("TextButton") then
				if i:FindFirstChild("Tool") then
					if i.Tool.BG.Item.Value == child then
						i.Tool:Destroy()
					end
				end
			end
		end
	else
		local tool = nil
		for _,i in pairs(script.Parent.BG:GetChildren()) do
			if i:IsA("TextButton") then
				if i:FindFirstChild("Tool") then
					if i.Tool.BG.Item.Value == child then
						tool = i
					end
				end
			end
		end
		if tool then
			tool.Tool.BG.ItemOrder.TextColor3 = Color3.fromRGB(47, 182, 17)
		end
	end
end)
game.Players.LocalPlayer.Character.ChildRemoved:Connect(function(child)
	if child.Parent ~= game.Players.LocalPlayer.Backpack then
		for _,i in pairs(script.Parent.BG:GetChildren()) do
			if i:IsA("TextButton") then
				if i:FindFirstChild("Tool") then
					if i.Tool.BG.Item.Value == child then
						i.Tool:Destroy()
					end
				end
			end
		end
	end
end)

for _,i in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
	Add(i)
end

UIS.InputBegan:Connect(function(key,inchat)
	if inchat == false then
		if nums[tostring(string.sub(tostring(key.KeyCode),14,#tostring(key.KeyCode)))] then
			if script.Parent.Visible == true and script.Parent.Parent.Enabled == true then
				local num = nums[tostring(string.sub(tostring(key.KeyCode),14,#tostring(key.KeyCode)))]
				local chosen = nil
				for _,i in pairs(script.Parent.BG:GetChildren()) do
					if i:IsA("TextButton") then
						if i:FindFirstChild("Tool") then
							if i.Tool.BG.Number.Value == tonumber(num) then
								chosen = i
							end
						end
					end
				end
				if chosen then
					if chosen.Tool.BG.Item.Value then
						if db == false then
							db = true
							if chosen.Tool.BG.Item.Value == game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool") then
								game.ReplicatedStorage.Files.RemoteEvents.Tool.UnequipTool:FireServer(game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool"))
							else
								if game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool") then
									local unequip = game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool")
									game.ReplicatedStorage.Files.RemoteEvents.Tool.UnequipTool:FireServer(unequip)
								end
								if chosen then
									game.ReplicatedStorage.Files.RemoteEvents.Tool.EquipTool:FireServer(chosen.Tool.BG.Item.Value)
								end	
							end
							wait()
							db = false
						end
					end
				end
			end
		end
	end
end)

The maint part is at line 221, adding wait doesnt fix the issue, I even added wait to the for i’s, but that just delays too much!

nevermind i fixed it with debounce