Script returns wrong numbers when changed

I have been working on a booga booga fangame, and I have a major problem. Backpack items will not return the correct number of items.
Script:

local function Ready(C:Model)
	if script.Parent:FindFirstChild(C.Name) then
		script.Parent:FindFirstChild(C.Name).Count.Text = tonumber(script.Parent:FindFirstChild(C.Name).Count.Text) + 1
		C.Destroying:Connect(function()
			script.Parent:FindFirstChild(C.Name).Count.Text = tonumber(script.Parent:FindFirstChild(C.Name).Count.Text) - 1
			if tonumber(script.Parent:FindFirstChild(C.Name).Count.Text) <= 0 then
				script.Parent:FindFirstChild(C.Name):Destroy()
			end
		end)
	else
		local Slot = game.ReplicatedStorage.Slot:Clone()
		Slot.Parent = script.Parent
		Slot.TextLabel.Text = C.Name
		Slot.Name = C.Name
		Slot.Count.Text = 1
		local Icon = C:Clone()
		Icon.Parent = Slot.ViewportFrame
		Icon:PivotTo(CFrame.new())
		Slot.Activated:Connect(function()
			Slot.Count.Text = tonumber(Slot.Count.Text) - 1
			game.ReplicatedStorage.EmptyObject:FireServer(game.Players.LocalPlayer.Inventory:FindFirstChild(C.Name))
			if tonumber(Slot.Count.Text) <= 0 then
				Slot:Destroy()
			end
		end)
		C.Destroying:Connect(function()
			Slot.Count.Text = tonumber(Slot.Count.Text) - 1
			if tonumber(Slot.Count.Text) <= 0 then
				Slot:Destroy()
			end
		end)
		--[[C.Changed:Connect(function(Prop)
			if Prop == "Parent" then
				Slot.Count.Text = tonumber(Slot.Count.Text) - 1
				if tonumber(Slot.Count.Text) <= 0 then
					Slot:Destroy()
				end
			end
		end)]]
	end
end
game.Players.LocalPlayer.Inventory.ChildAdded:Connect(function(C)
	Ready(C)
end)
for i, obj in pairs(game.Players.LocalPlayer:WaitForChild("Inventory"):GetChildren()) do
	Ready(obj)
end
1 Like