Need ImageLabels To Change When Button Pressed

This script is not going to be the final outcome and I’m just testing it so I can adjust it and use for something properly, however, I’m having trouble finding out how to change the images when a button is pressed.

My intention is:

  1. Button is pressed, script checks for “ImageLabel” inside of the children of the “Images” folder.
  2. Script changes all of the ImageLabel’s image when pressed.

I have provided a video and the script that is inside of the TextButton below. The script does not work as expected and doesn’t return any errors in the output window.

local images = script.Parent.Parent.Images

for _, child in ipairs(images:GetChildren()) do
	if child.Name == "ImageLabel" then
		changeImage = child
		break
	end
end

script.Parent.MouseButton1Click:Connect(function()
	if changeImage then
		print("Found ImageLabel!")
		changeImage.Image = "http://www.roblox.com/asset/?id=17316690696"
	else
		print("ImageLabel not found")
	end
end)

3 Likes

Forgot to show the order.
image

GetChildren() only includes any instance that are directly a child of the Images folder, not its grandchildren (or descendants). While GetDescendants() might be something you want to use, you might probably add another image, like a small icon in the future, which you don’t want its asset ID to change.

Instead, access it directly:

local imageLink = "rbxassetid://17316690696"

script.Parent.MouseButton1Click:Connect(function()
	for _, child in ipairs(images:GetChildren()) do
		local image = child:FindFirstChild("ImageLabel") -- you might want to use a more unique name, like "ItemPreviewIcon"
		
		if (image == nil) then -- if the child does not contain an object named "ImageLabel" (doesn't matter what class though)
			continue -- skip
		end
		
		image.Image = imageLink
	end
end)
1 Like

Hey there, thanks alot. I was wondering how I would define null?

1 Like

null, as in the value? In Lua, it is nil`.

local nilValue = nil

Haha, I just saw what was wrong. My mind was in another language, my bad.

2 Likes

No problem lol, anyways, thanks alot this worked.

I’ve been picking at this for hours and couldn’t figure it out for some reason. I’ll note to use GetChildren() too instead for similar instances. Thanks alot, again!

1 Like

Hey, really sorry. So I used the sample code and turned it into a function into the main game/script I’ll be using it for and I’m a bit confused as to why it’s not working now.

I adjusted it to fit properly, the function is;

local function changeImage()
	local imageLink = "rbxassetid://17316025753"
	local parent = script.Parent.Parent

	script.Parent.MouseButton1Click:Connect(function()
		for _, child in ipairs(parent:GetChildren()) do
			local image = child:FindFirstChild("EquipButton")

			if (image == nil) then
				continue
			end

			image.Image = imageLink
		end
	end)
end

For context, what I’m trying to do is a shop where when a player equips an item, it sets all of the “EquipButton”'s images to the original “EQUIP” image except for the button they pressed.

The function is called in the script below;

equipButton.MouseButton1Click:Connect(function()
	clickSound:Play()
	if equipButton.Image == "rbxassetid://17316025753" then
		if sword then
			checkAndReplaceSword()
			changeImage()
			local swordClone = sword:Clone()
			swordClone.Parent = Players.LocalPlayer.Backpack
			equipButton.Image = "rbxassetid://17316048949"
		end
	elseif equipButton.Image == "rbxassetid://17316048949" then
		local equippedSword = Players.LocalPlayer.Backpack:FindFirstChild(sword_to_give)
		if equippedSword then
			equippedSword:Destroy()
			equipButton.Image = "rbxassetid://17316025753"
		end
	end
end)

The images do not adjust, as can be seen in the video below. I know it’s not really similar to the original topic so respond if you wish but I’d appreciate any help you may have, thanks again.

No errors in output window.

The code will still function, just not in an expected way. Since changeImage is still connecting another function to the same button’s MouseButton1Click event, the image will only change when that button is pressed, but not before changeImage is ran.

I assumed by simply removing the connection and function block used in the previous example you can achieve what you’re expecting changeImage to do:

local function changeImage()
	local imageLink = "rbxassetid://17316025753"
	local parent = script.Parent.Parent

	for _, child in ipairs(parent:GetChildren()) do
		local image = child:FindFirstChild("EquipButton")

		if (image == nil) then
			continue
		end

		image.Image = imageLink
	end
end
2 Likes

Thank you so much, this worked. I appreciate your extended help and efforts in helping me, I’ve learnt something new too. Have a great day.

1 Like

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