Script does not change image of specific ImageButton

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am working on an inventory system for a game. The way it works is that when a player picks up an item, the item gets added to a table. The index of the item corresponds to the ImageButtons name (first item in the table changes the image of the ImageButton object named “1”). When an item gets dropped it gets removed from the table, the images that were behind the dropped item will move forward and take the empty space infront of them.

  2. What is the issue? Include screenshots / videos if possible!
    The slots 3 and 4 do not seem to change images even though there is another/no item in the table that has the index 3 or 4, every other slot works except those two.

Here is a video of the problem
The red bouncy balls on slot 3 and 4 presist even though they have been removed from the table

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked but as far as I have searched, there was no one who had the same issue as me. I reviewed my own code and its working perfectly fine except for the slots 3 and 4

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

This is the code that is responsible for the removal of images

dropItem.OnClientEvent:Connect(function(item)
	
	if item ~= "Gadget" and item ~= "Armor" then
		
		table.remove(invItems, item)
		
		for i, slot in pairs(frame:GetChildren()) do
			
			local num = tonumber(slot.Name)
			
			if not slot:IsA("ImageButton") then return end
			
			if invItems[num] then
				
				for itemName, id in pairs(itemImgs) do

					if itemName == invItems[num].Name then

						slot.Image = id

					end

				end
				
			else
				
				print(slot.Name)
				
				slot.Image = "rbxassetid://0"
			
			end
			
		end
		
	else
		
		local slot = frame:FindFirstChild(item)
		slot.Image = "rbxassetid://0"
		
	end
	
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Can you show screenshot of the explorer that contain those Gui with all childs?

1 Like

grafik

Here you go

1 Like

Couldn’t you just set the image transparency to 1 then whenever you set an actual Id just set the transparency to be 0

2 Likes

I have tried but it still doesn’t work. I don’t know why but the script just refuses to change any value of 3 and 4.

1 Like

Alright, I managed to fix the issue.
The issue was that the GetChildren() got the children by the order they got added to the frame instead of how they are sorted. The armor and gadget ImageButtons were for whatever reason indexed before 3 and 4, causing the num variable to return nil and not changing 3 and 4.
I had to remove everything from the frame and drag everything back in the order I needed to. I don’t know why GetChildren() works like that.

2 Likes

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