When player equips a newskin or trap, the "cost" doesn't change for the original equipped item

Hi, whenever the player equips something new, the original equipped item doesn’t change to “owned” Here is my code:

		frame.Button.MouseButton1Click:Connect(function()
			local result = game.ReplicatedStorage.BuyItem:InvokeServer(v.Name, "skin")
			
			if result == "bought" then
				frame.Button.Image = greenTick
				frame.Cost.Text = "Owned"
				
			elseif result == "equipped" then

				
				for _, object in pairs(skins.Folder:GetChildren()) do
					if object:IsA("Frame") and object:FindFirstChild("Cost") then
						if game.Players.LocalPlayer.SkinInventory:FindFirstChild(object.Name) then
							object.Cost.Text = "Owned"
						end
					end
				end
				frame.Button.Image = greenTick
				frame.Cost.Text = "Equipped"
			end

		end)

Here is the explorer:

image

Adding onto the issue, what happens is when the player equips the new skin/trap. It doesn’t replace the other cost values to “owned”

You never put “frame” as a variable, and that’s is why the code won’t run.

                                                       '''
           
         local frame = game.StarterGui.ScreenGui.Frame  
             -- Code
      end

/ ‘’’

I did, just didn’t include it. here’s the full code:

local shop = script.Parent

local skins = shop:WaitForChild("Skins")
local traps = shop:WaitForChild("Traps")
local gamepasses = shop:WaitForChild("Gamepasses")

local skinsBtn = shop:WaitForChild("SkinsButton")
local trapsBtn = shop:WaitForChild("TrapsButton")
local gamepassBtn = shop:WaitForChild("GamepassesButton")

local itemTemplate = script:WaitForChild("Template")

local cart = "http://www.roblox.com/asset/?id=3130587434"
local greenTick = "http://www.roblox.com/asset/?id=53252229"

local Module3D = require(game.ReplicatedStorage:WaitForChild("Module3D"))

local enabledTextTransparency = 0
local disabledTextTransparency = 0.5

skinsBtn.MouseButton1Click:Connect(function()
	traps.Visible = false
	gamepasses.Visible = false
	skins.Visible = true
	trapsBtn.TextTransparency = disabledTextTransparency
	gamepassBtn.TextTransparency = disabledTextTransparency
	skinsBtn.TextTransparency = enabledTextTransparency
end)

trapsBtn.MouseButton1Click:Connect(function()
	skins.Visible = false
	gamepasses.Visible = false
	traps.Visible = true
	gamepassBtn.TextTransparency = disabledTextTransparency
	skinsBtn.TextTransparency = disabledTextTransparency
	trapsBtn.TextTransparency = enabledTextTransparency
end)
gamepassBtn.MouseButton1Click:Connect(function()
	traps.Visible = false
	skins.Visible = false
	gamepasses.Visible = true
	trapsBtn.TextTransparency = disabledTextTransparency
	skinsBtn.TextTransparency = disabledTextTransparency
	gamepassBtn.TextTransparency = enabledTextTransparency
end)


local skinsData = game.ReplicatedStorage.Skins:GetChildren()
local trapsData = game.ReplicatedStorage.Traps:GetChildren()

function createFrame(name, cost, object, parent, itemType)
	local frame = itemTemplate:Clone()
	frame.Name = name
	frame.Title.Text = name
	frame.Cost.Text = cost
	
	
	if itemType == "skin" then
		local skinModel3D = Module3D:Attach3D(frame.ViewportFrame, object)
		skinModel3D:SetDepthMultiplier(1.2)
		skinModel3D.Camera.FieldOfView = 5
		skinModel3D.Visible = true
		
		game:GetService("RunService").RenderStepped:Connect(function()
			skinModel3D:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
		end)
	elseif itemType == "trap" then
		local trapModel3D = Module3D:Attach3D(frame.ViewportFrame, object)
		trapModel3D:SetDepthMultiplier(1.2)
		trapModel3D.Camera.FieldOfView = 5
		trapModel3D.Visible = true
		
		game:GetService("RunService").RenderStepped:Connect(function()
			trapModel3D:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
		end)	
	end
	
	
	frame.Parent = parent
	
	return frame
end

function addSkins(data)
	for i, v in pairs(data) do
		local frame = createFrame(v.Name, v.Cost.Value, v, skins.Folder, "skin")
		if game.Players.LocalPlayer.SkinInventory:FindFirstChild(v.Name) then
			frame.Cost.Text = "Owned"
			frame.Button.Image = greenTick
		end
		
		if game.Players.LocalPlayer.equippedSkin.Value == v.Name then
			frame.Cost.Text = "Equipped"
			frame.Button.Image = greenTick
		end
		
		frame.Button.MouseButton1Click:Connect(function()
			local result = game.ReplicatedStorage.BuyItem:InvokeServer(v.Name, "skin")
			
			if result == "bought" then
				frame.Button.Image = greenTick
				frame.Cost.Text = "Owned"
				
			elseif result == "equipped" then

				
				for _, object in pairs(skins.Folder:GetChildren()) do
					if object:IsA("Frame") and object:FindFirstChild("Cost") then
						if game.Players.LocalPlayer.SkinInventory:FindFirstChild(object.Name) then
							object.Cost.Text = "Owned"
						end
					end
				end
				frame.Button.Image = greenTick
				frame.Cost.Text = "Equipped"
			end

		end)
	end
end

function addTraps(data)
	for i, v in pairs(data) do
		local frame = createFrame(v.Name, v.Cost.Value, v, traps.Folder, "trap")
		if game.Players.LocalPlayer.TrapInventory:FindFirstChild(v.Name) then
			frame.Cost.Text = "Owned"
			frame.Button.Image = greenTick
		end
		
		if game.Players.LocalPlayer.equippedTrap.Value == v.Name then
			frame.Cost.Text = "Equipped"
			frame.Button.Image = greenTick
		end
		
		frame.Button.MouseButton1Click:Connect(function()
			local result = game.ReplicatedStorage.BuyItem:InvokeServer(v.Name, "trap")
			
			if result == "bought" then
				frame.Button.Image = greenTick
				frame.Cost.Text = "Owned"
				
			elseif result == "equipped" then

				
				for _, object in pairs(traps.Folder:GetChildren()) do
					if object:IsA("Frame") and object:FindFirstChild("Cost") then
						if game.Players.LocalPlayer.TrapInventory:FindFirstChild(object.Name) then
							object.Cost.Text = "Owned"
						end
					end
				end
				frame.Button.Image = greenTick
				frame.Cost.Text = "Equipped"
			end

		end)
	end
end

game.ReplicatedStorage.SendData.OnClientEvent:Connect(function()
	addSkins(skinsData)
	addTraps(trapsData)
end)

Can you show your explorer? Maybe if you do I can help.

Here
image

Maybe try adding a remote event that fires when the player clicks the button?

There is one in my handling script, would you like that script too?

Here’s the event handling script

game.ReplicatedStorage.BuyItem.OnServerInvoke = function(player, itemName, itemType)
	
	local DataStore2 = require(game.ServerScriptService.DataStore2)
	local MarketplaceService = game:GetService("MarketplaceService")
	
	local defaultCashValue = 0
	
	
	game.Players.PlayerAdded:Connect(function(plr)
		local coinsDataStore = DataStore2("Coins", player)
		
	
		
		local cash = Instance.new("StringValue")
		cash.Name = "Cash"
		
		local function coinsUpdated(updatedValue)
			cash.Value = coinsDataStore:Get(updatedValue)
			game.ReplicatedStorage.UpdateClientCurrency:FireClient(plr, coinsDataStore:Get(defaultCashValue))
	
		end
		
		coinsUpdated(defaultCashValue)
		
		coinsDataStore:OnUpdate(coinsUpdated)
		
		
	end)
	
	local item
	local inInventory
	
	if itemType == "skin" then
		item = game.ReplicatedStorage.Skins:FindFirstChild(itemName)
		
		if player.SkinInventory:FindFirstChild(itemName) then
			inInventory = true
		end
	elseif itemType == "trap" then
		item = game.ReplicatedStorage.Traps:FindFirstChild(itemName)
		if player.TrapInventory:FindFirstChild(itemName) then
			inInventory = true
		end
	end
	
	if item then
		local coinsDataStore = DataStore2("Coins", player)
		if item:FindFirstChild("Cost") then
			if not inInventory then
				print("Not in player's inventory")
				if item.Cost.Value <=  coinsDataStore:Get() then
					print("Player can buy this")
					coinsDataStore:Increment(-item.Cost.Value)
					local stringValue = Instance.new("StringValue")
					stringValue.Name = item.Name
					
					if itemType == "skin" then
						stringValue.Parent = player.SkinInventory
					elseif itemType == "trap" then
						stringValue.Parent = player.TrapInventory
					end
					
					return "bought"
				else
					return "failed"
				end
			else
				print("Player already owns the item")
				
				if itemType == "skin" then
					player.equippedSkin.Value = itemName
				elseif itemType == "trap" then
					player.equippedTrap.Value = itemName  
				end
				return "equipped"
			end
		end
	else
		print("No skin/trap found")
		return "failed"
	end
end

Are you storing an amount in a string value?

If so…why? Just use an IntValue…