Need the ImageButton Images to change in function

Hi there, I’m not a scripter however I’ve gotten some help with this script from DevForum and I wanted to add something within the function. The idea is so when the function is called, check if a tool exists in the players backpack, destroy it and then change the image of all the parent’s children.

A part of the script I’m using is:

local parent = script.Parent.Parent
local children = parent:GetChildren()

local equipButtonFind = parent:FindFirstChild("EquipButton")

for _, child in ipairs(swordStorage:GetChildren()) do
	childrenNames[child.Name] = true
end

local function checkAndReplaceSword()
	local backpack = player.Backpack
	for _, tool in ipairs(backpack:GetChildren()) do
		if childrenNames[tool.Name] then
			tool:Destroy()
			if equipButtonFind then
				equipButtonFind.Image = "rbxassetid://17316025753"
			end
		end
	end

image
:arrow_heading_up: This is the layout of where everything is placed, I want the EquipButton image to be changed.

image
:arrow_heading_up: There are multiple like this with an EquipButton inside of them.

The tool destroying works, however, I want the EquipButton image to change too. The script doesn’t return any errors. Any help would be appreciated.

I have also already tried findObjectByName.

Still need help wtih this if possible thanks :smile:

Destroying the tool may work, but where is the GUI located when the tool is equipped? It may be cloned into the player and destroying the tool doesn’t destroy the GUI.
In test mode find out where it’s physically located in the player when the tool is equipped. Make sure when your script destroys the tool it also destroys that GUI in the correct location.

I’m confused. I’m not having issues destroying the tool nor do I want to destroy the UI, I want the images of the “EquipButton” in all of “ScrollingFrame”'s children to be changed when this function is called.

Still really need assistance on this if anyone can help, I can provide more detail and answer any questions. Desperately need help…

It looks like you shown the whole code though. I can see where you find the image, but not anything else you are doing with it.

local function checkAndReplaceSword()
	local backpack = player.Backpack
	for _, tool in ipairs(backpack:GetChildren()) do
		if childrenNames[tool.Name] then
			tool:Destroy()
			if equipButtonFind then
				equipButtonFind.Image = "rbxassetid://17316025753"
			end
		end
	end

[/quote]

Full script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local buyButton = script.Parent
local equipButton = script.Parent.EquipButton
local clickSound = ReplicatedStorage.GameSounds.ClickSound
local hoverSound = ReplicatedStorage.GameSounds.HoverSound

local sword_to_give = script.Parent.Name
local sword = ReplicatedStorage.Swords:FindFirstChild(sword_to_give)
local gemCost = script.Parent.Price.Value

local buyButtonConnection
local yesButtonConnection
local uiElement = Players.LocalPlayer.PlayerGui:WaitForChild("User Interface"):WaitForChild("UserUI").SwordPurchaseUI

local player = Players.LocalPlayer
local swordStorage = game.ReplicatedStorage:WaitForChild("Swords")
local childrenNames = {}


local parent = script.Parent.Parent
local children = parent:GetChildren()

local equipButtonFind = parent:FindFirstChild("EquipButton")

for _, child in ipairs(swordStorage:GetChildren()) do
	childrenNames[child.Name] = true
end

local function checkAndReplaceSword()
	local backpack = player.Backpack
	for _, tool in ipairs(backpack:GetChildren()) do
		if childrenNames[tool.Name] then
			tool:Destroy()
			if equipButtonFind then
				equipButtonFind.Image = "rbxassetid://17316025753"
			end
		end
	end
end


checkAndReplaceSword()

local function TweenUI()
	uiElement.Position = UDim2.new(0.5, 0, 1.5, 0)
	uiElement.Size = UDim2.new(0, 0, 0, 0)
	uiElement.Visible = true
	game.Lighting.Blur.Enabled = true

	local tweenInfo = TweenInfo.new(
		1,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)

	local targetProperties = {
		Position = UDim2.new(0.275, 0,0.258, 0),
		Size = UDim2.new(0, 589,0, 387),
	}

	local tween = TweenService:Create(uiElement, tweenInfo, targetProperties)
	tween:Play()
end

script.Parent.MouseEnter:Connect(function()
	hoverSound:Play()
end)

local function buyItem()
	local player = Players.LocalPlayer
	local playerGui = player.PlayerGui
	clickSound:Play()
	script.Parent.Parent.Parent.Visible = false

	local confirmationUI = playerGui["User Interface"].UserUI.SwordConfirmationUI
	confirmationUI.SwordName.Text = sword_to_give

	if confirmationUI then
		confirmationUI.Visible = true
		confirmationUI.GemsCost.Text = gemCost
		confirmationUI.SwordImage_Outline.Image = script.Parent.ImageLabel.Image
		confirmationUI.SwordImage.Image = confirmationUI.SwordImage_Outline.Image
		game.Lighting.Blur.Enabled = true

		local yesButton = confirmationUI:FindFirstChild("YesButton")
		if yesButton then
			yesButtonConnection = yesButton.MouseButton1Click:Connect(function()
				if player.leaderstats.Gems.Value >= gemCost then
					confirmationUI.Visible = false
					equipButton.Visible = true
					player.leaderstats.Gems.Value = player.leaderstats.Gems.Value - gemCost
					buyButtonConnection:Disconnect()
					yesButtonConnection:Disconnect()
					playerGui.Frames.FramesUI.SwordsFrame.Visible = false
					clickSound:Play()
					uiElement.GemCost.Text = gemCost
					uiElement.SwordName.Text = sword_to_give
					uiElement.SwordImage.Image = script.Parent.ImageLabel.Image
					TweenUI()
					ReplicatedStorage.GameSounds.PurchaseSound:Play()
				else
					confirmationUI.YesButton.Image = "rbxassetid://17330634285"
					wait(2)
					confirmationUI.YesButton.Image = "rbxassetid://17324203359"
				end
			end)
		end

		local noButton = confirmationUI:FindFirstChild("NoButton")
		if noButton then
			noButton.MouseButton1Click:Connect(function()
				if yesButtonConnection then
					yesButtonConnection:Disconnect()
				end
				confirmationUI.Visible = false
				clickSound:Play()
				script.Parent.Parent.Parent.Visible = true
				game.Lighting.Blur.Enabled = false
			end)
		end
	end
end

buyButtonConnection = buyButton.MouseButton1Click:Connect(buyItem)

equipButton.MouseEnter:Connect(function()
	hoverSound:Play()
end)

equipButton.MouseButton1Click:Connect(function()
	clickSound:Play()
	if equipButton.Image == "rbxassetid://17316025753" then
		if sword then
			checkAndReplaceSword() - - Where the function gets called
			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 more relevant scripts are near the top and bottom, I also attached a video of me testing. When I click on one equip button, the intention is for all the others images to be set to the “EQUIP” button image and the button that was clicked be set to “UNEQUIP”