Whenever I buy a tool that is after another tool, it buys the tools before it. But if I buy the first tool, it works normally

Client:

-- Services
local RS = game:GetService("ReplicatedStorage")
local PlayerS = game:GetService("Players")

-- Coins
local Coins = PlayerS.LocalPlayer.Coins.Value

-- Remote Event
local ShopEvent = RS:WaitForChild("ShopEvent")
local AlreadyBought = RS:WaitForChild("AlreadyBoughtEvent")

-- Player
local player = PlayerS.LocalPlayer
local bag = player.Backpack

-- GUI
local gui = script.Parent
local shop = gui.Shop
local desc = shop.ItemDescription
local toolSF = shop.ToolsScrollingFrame
local buyB = desc.BuyButton
local closeB = shop.CloseButtonF.CloseButton
local toolT = {}

-- Item Description content
local itemName = desc.ItemName
local itemDmg = desc.Damage
local itemPrice = desc.Price
local itemAbility = desc.Ability
local itemImg = desc.ItemImage

local function deleteValues()
	itemName.Text = "No Item Selected"
	itemDmg.Text = "Damage: N/A"
	itemPrice.Text =  "Price: N/A"
	itemAbility.Text = "Ability: N/A"
	itemImg.Image = ""
end

local selected = nil

table.insert(toolT, toolSF:GetChildren())


for i, descendant in pairs(toolSF:GetDescendants()) do 
	if descendant:IsA("ImageButton") then
		boughtVal = descendant.AlreadyBoughtVal
		descendant.MouseButton1Click:Connect(function()
			
			itemName.Text = descendant.ItemName.Value
			itemDmg.Text = "Damage: ".. descendant.Damage.Value
			itemPrice.Text = "Price: $".. descendant.Price.Value
			itemAbility.Text = "Ability: ".. descendant.Ability.Value
			itemImg.Image = descendant.ImageVal.Value
			selected = descendant
		end)
	end
end

buyB.MouseButton1Click:Connect(function()
	if boughtVal.Value == false then
		ShopEvent:FireServer(selected)
		print(selected.Name .. " has been fired")
		task.wait(1)
	elseif boughtVal.Value == true then
		warn(selected.Name .. " is already owned")
	end
end)

local function itemBought(alreadybought)
	buyB.BackgroundColor3 = Color3.fromRGB(39, 16, 108)
	buyB.TextColor3 = Color3.fromRGB(255, 255, 255)
	buyB.Text = "Already Bought"
	alreadybought.BoughtPattern.Visible = true
end

closeB.MouseButton1Click:Connect(deleteValues)
AlreadyBought.OnClientEvent:Connect(itemBought)

Server:

-- Services
local RS = game:GetService("ReplicatedStorage")
local PlayerS = game:GetService("Players")
local SS = game:GetService("ServerStorage")

-- Remote Event
local ShopEvent = RS:WaitForChild("ShopEvent")
local AlreadyBought = RS:WaitForChild("AlreadyBoughtEvent")

-- Tools
local tools = SS:WaitForChild("Tools")


function onEventRecieved(player, selected)
print(selected.Name)
if selected.AlreadyBoughtVal.Value == false then
	local Coins = player.Coins.Value
		if Coins >= selected.Price.Value then
			Coins = Coins - selected.Price.Value
	
			local toolGiven = tools:FindFirstChildOfClass("Tool") and tools:FindFirstChild(selected.Name)
			print(toolGiven)
			local toolClone = toolGiven:Clone()
			
			toolClone.Parent = player.Backpack
			
			selected.AlreadyBoughtVal.Value = true
			print(selected.Name .. " has been bought")
			AlreadyBought:FireClient(player, selected)
			task.wait(1)
			
			elseif Coins <= selected.Price.Value then
				warn("Not Enough Money")
			return
		end

	end
end
ShopEvent.OnServerEvent:Connect(onEventRecieved)

It prints how it should print, but the action is different:
image

Video:

I’ve changed the code so much that I’m tired lol. Thanks for helping

You should put boughtVal = descendant.AlreadyBoughtVal inside the MouseButton1Click event, otherwise it would always pick the bought value of the last item.

alright then, I will try that!

That works, but if I buy the axe first, I can still buy the sword, BUT the buy button text is showing already bought still

Because you scripted it that way:

On the mouseclick event, you could add some extra code:

This way you can make sure the buy button text depends on the item you click on.

2 Likes