Weird bug with imagebuttons some one please help!

https://medal.tv/games/roblox-studio/clips/b080YonP9nGAi/d1337nLxbDlH?invite=cr-MSxVMnIsMjQ5MTMwNTks

Basically when I click on an item in my inventory and then click on another item, then I drop that item, both of the items drop. Sorry if that’s confusing, the clip will help you understand. Anyways I cant seem to find a solution to this problem. I tried using coroutine.yield() but that didn’t work for me. The problem arises from the mouse click event.

local script~

local slots = script.Parent.Parent.Slots
local player = game.Players.LocalPlayer
local text = ""

for _, button in ipairs(slots:GetChildren()) do
	button.Visible = false
	button.Item.Changed:Connect(function(item)
		if item == "empty" then
			button.Visible = false
			button.Image = "0"
			button.Item.Value = "empty"
			button.Parent.Parent.items:FindFirstChild(button.Name)
		elseif item == "Wood" then
			button.Image = "rbxassetid://8812775718"
			button.Visible = true
			local wood = Instance.new("StringValue")
			wood.Name = button.Name
			wood.Value = "Wood"
			wood.Parent = button.Parent.Parent.items
			text = "A hardy and chunky piece of wood."
		elseif item == "Fiber" then
			button.Image = "rbxassetid://8812843259"
			button.Visible = true
			local fiber = Instance.new("StringValue")
			fiber.Name = button.Name
			fiber.Value = "Fiber"
			fiber.Parent = button.Parent.Parent.items
			text = "A strand of fiber."
		elseif item == "Rock" then
			button.Image = "rbxassetid://8812849092"
			button.Visible = true
			local Rock = Instance.new("StringValue")
			Rock.Name = button.Name
			Rock.Value = "Rock"
			Rock.Parent = button.Parent.Parent.items
			text = "A rough Rock."
		end
	end)
	button.MouseButton1Click:Connect(function()
		
		local imgframe = button
		
		if button.Item.Value == "Wood" then
			text = "A hardy and chunky piece of wood."
		elseif button.Item.Value == "Rock" then
			text = "A rough Rock."
		elseif button.Item.Value == "Fiber" then
			text = "A strand of fiber."
		end
		
		button.Parent.Parent.aboit.Text = text
		button.Parent.Parent.Drop.Visible = true
		
		button.Parent.Parent.Drop.drop.Activated:Connect(function()
			local humc = player.Character.HumanoidRootPart.CFrame
			local offset = CFrame.new(0, 0, -2)
			local org = game.ReplicatedStorage:FindFirstChild(button.Item.Value)
			local copy = org:Clone()
			copy.Parent = game.Workspace
			copy.CFrame = humc*offset
			button.Item.Value = "empty"
			button.Parent.Parent.Drop.Visible = false
			button.Parent.Parent.aboit.Text = ""
		end)
	end)
end```

--Thank you for your time, any tips will be deeply appreciated!

The issue is honestly fairly simple but may be difficult to explain depending on your level of understanding when it comes to scripting. In short, the drop.Activated function is nested within the click function, hence, when the button is clicked, the drop function is created for the given button. Even though you may not see it, after clicking both the buttons, you have two active versions of the drop function.

A clean fix would require a major rewrite of your code, but in short, you should only really have one drop function, considering you only have one drop button (or so it seems). If you want to keep most everything the same I would just have a value containing the button that was last clicked and then deleting it whenever the drop button is clicked. The drop function can be placed outside the loop, again assuming you have one.

1 Like

I think I understand but having a value value of the button that is clicked and then it being deleted when the drop button is clicked doesn’t seem like it will fix the problem, but I understand everything else thank you!

Sorry, I was a bit rushed and was referring to the value containing the button last pressed and resetting it. Either way, may not be what you needed, but as long as you understood the meat of what I explained, you should be fine.

1 Like

I figured it out I can’t thank you enough, here is the final script if you are curious.

local slots = script.Parent.Parent.Slots
local player = game.Players.LocalPlayer
local text = ""
local buttonclicked = nil

for _, button in ipairs(slots:GetChildren()) do
	button.Visible = false
	button.Item.Changed:Connect(function(item)
		if item == "empty" then
			button.Visible = false
			button.Image = "0"
			button.Item.Value = "empty"
			button.Parent.Parent.items:FindFirstChild(button.Name)
		elseif item == "Wood" then
			button.Image = "rbxassetid://8812775718"
			button.Visible = true
			local wood = Instance.new("StringValue")
			wood.Name = button.Name
			wood.Value = "Wood"
			wood.Parent = button.Parent.Parent.items
			text = "A hardy and chunky piece of wood."
		elseif item == "Fiber" then
			button.Image = "rbxassetid://8812843259"
			button.Visible = true
			local fiber = Instance.new("StringValue")
			fiber.Name = button.Name
			fiber.Value = "Fiber"
			fiber.Parent = button.Parent.Parent.items
			text = "A strand of fiber."
		elseif item == "Rock" then
			button.Image = "rbxassetid://8812849092"
			button.Visible = true
			local Rock = Instance.new("StringValue")
			Rock.Name = button.Name
			Rock.Value = "Rock"
			Rock.Parent = button.Parent.Parent.items
			text = "A rough Rock."
		end
	end)
	button.MouseButton1Click:Connect(function()
		buttonclicked = button
		balls()
		local imgframe = button
		print(button)
		if button.Item.Value == "Wood" then
			text = "A hardy and chunky piece of wood."
		elseif button.Item.Value == "Rock" then
			text = "A rough Rock."
		elseif button.Item.Value == "Fiber" then
			text = "A strand of fiber."
		end
		
		button.Parent.Parent.aboit.Text = text
		button.Parent.Parent.Drop.Visible = true
	end)
end

function balls()
	if buttonclicked ~= nil then
		buttonclicked.Parent.Parent.Drop.drop.Activated:Connect(function()
			local humc = player.Character.HumanoidRootPart.CFrame
			local offset = CFrame.new(0, 0, -2)
			local org = game.ReplicatedStorage:WaitForChild(buttonclicked.Item.Value)
			local copy = org:Clone()
			copy.Parent = game.Workspace
			copy.CFrame = humc*offset
			buttonclicked.Item.Value = "empty"
			buttonclicked.Parent.Parent.Drop.Visible = false
			buttonclicked.Parent.Parent.aboit.Text = ""
		end)
	end
end