Object count of item changes are very buggy with every pickup

So I’m making an inventory system and everything is working fine except for this annoying bug that I can’t fix. I tried everything but it wont work.

Picture of explorer:

Video of the bug:
robloxapp-20240714-1722277.wmv (2.8 MB)

Pickup script:

local cas = game:GetService('ContextActionService')
local uis = game:GetService('UserInputService')
local ts = game:GetService('TweenService')
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local InventoryGUI = script.Parent

function addtoframe(item)
	if not InventoryGUI.Inventory:FindFirstChild(item.Name) then
		local itemframe = InventoryGUI.Parent.Template:Clone()
		itemframe.Name = item.Name
		itemframe.Parent = InventoryGUI.Inventory
		itemframe.Info.Text = item.Name
		item.Parent = itemframe.Viewport
		
		local itemcam = Instance.new("Camera",itemframe.Viewport)
		itemcam.CFrame = CFrame.new(item.Position + (item.CFrame.lookVector * 3.5),item.Position)
		itemframe.Viewport.CurrentCamera = itemcam
		
		local objectsLabel = itemframe.Objects
		objectsLabel:SetAttribute("Objects",1)
		objectsLabel.Text = tostring(objectsLabel:GetAttribute("Objects"))
	else
		local objectsLabel = InventoryGUI.Inventory[item.Name].Objects
		local num = objectsLabel:GetAttribute("Objects") + 1
		objectsLabel:SetAttribute("Objects", num)
		objectsLabel.Text = tostring(objectsLabel:GetAttribute("Objects"))
	end
end

function pickupitem(an,is,io,item)
	if is == Enum.UserInputState.Begin then
		item.Parent = workspace
		item.CanCollide = false
		local tween = ts:Create(item,TweenInfo.new(.3,Enum.EasingStyle.Linear),{CFrame = player.Character.HumanoidRootPart.CFrame, Transparency = 1})
		tween:Play()
		tween.Completed:Wait()
		item:Destroy()
		addtoframe(game.ReplicatedStorage[item.Name]:Clone())
	end
end

uis.InputChanged:Connect(function(i,p)
	if p then return end
	if mouse.Target and mouse.Target.Parent.Name == "Pickables" and player:DistanceFromCharacter(mouse.Target.Position) < 30 then
		local item = mouse.Target
		cas:BindAction(
			"Pickup",
			function(an,is,io)
				pickupitem(an,is,io,item)
			end,
			false,
			Enum.KeyCode.E
		)
	else
		cas:UnbindAction("Pickup")
	end
end)

There is nothing wrong with my drop item script though
Please help out as it is ruining the whole system

Can you send the game file? I’ll try debugging it, since nothing really seems wrong or stands out at first glance.

Can you add some print statements to check if whenever you pick an item it actually gets to the line where you increase the quantity inside the inventory? Do something like this:

local cas = game:GetService('ContextActionService')
local uis = game:GetService('UserInputService')
local ts = game:GetService('TweenService')
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local InventoryGUI = script.Parent

function addtoframe(item)
	if not InventoryGUI.Inventory:FindFirstChild(item.Name) then
		print("Item already exists")
		
		local itemframe = InventoryGUI.Parent.Template:Clone()
		itemframe.Name = item.Name
		itemframe.Parent = InventoryGUI.Inventory
		itemframe.Info.Text = item.Name
		item.Parent = itemframe.Viewport

		local itemcam = Instance.new("Camera",itemframe.Viewport)
		itemcam.CFrame = CFrame.new(item.Position + (item.CFrame.lookVector * 3.5),item.Position)
		itemframe.Viewport.CurrentCamera = itemcam

		local objectsLabel = itemframe.Objects
		objectsLabel:SetAttribute("Objects",1)
		print(item.Name, objectsLabel:GetAttribute("Objects"))
		objectsLabel.Text = tostring(objectsLabel:GetAttribute("Objects"))
	else
		print("Adding new item")
		
		local objectsLabel = InventoryGUI.Inventory[item.Name].Objects
		local num = objectsLabel:GetAttribute("Objects") + 1
		objectsLabel:SetAttribute("Objects", num)
		print(item.Name, objectsLabel:GetAttribute("Objects"))
		objectsLabel.Text = tostring(objectsLabel:GetAttribute("Objects"))
	end
end

function pickupitem(an,is,io,item)
	if is == Enum.UserInputState.Begin then
		item.Parent = workspace
		item.CanCollide = false
		local tween = ts:Create(item,TweenInfo.new(.3,Enum.EasingStyle.Linear),{CFrame = player.Character.HumanoidRootPart.CFrame, Transparency = 1})
		tween:Play()
		tween.Completed:Wait()
		item:Destroy()
		
		print("Tween complete")
		
		addtoframe(game.ReplicatedStorage[item.Name]:Clone())
	end
end

uis.InputChanged:Connect(function(i,p)
	if p then return end
	if mouse.Target and mouse.Target.Parent.Name == "Pickables" and player:DistanceFromCharacter(mouse.Target.Position) < 30 then
		local item = mouse.Target
		cas:BindAction(
			"Pickup",
			function(an,is,io)
				pickupitem(an,is,io,item)
			end,
			false,
			Enum.KeyCode.E
		)
	else
		cas:UnbindAction("Pickup")
	end
end)

And send your output, because indeed there’s no clear reason to why that’s happening, at least I don’t see it. So we’ll have to debug to find out what’s going on.

Your print statements worked! I found out the problem when I experimented with the game a bit and realised the game updates the frame a little later than when the part’s tween finished. So the problem lies in the waiting

1 Like

Found the problem, I need to put a wait(1) instead of tween.Completed:Wait()

1 Like

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