Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i make it so if you already have the weapon, you can't purchase it again?

Basically, I have this shop GUI set up so that when you purchase something with enough money it gives you the item from replicated storage. The problem is that you can purchase it again, and I don't want that...

tl;dr -- How to make this so, after purchase, you can't purchase again?

local price = script.Parent.Parent.Price
local tools = game.ReplicatedStorage:WaitForChild("Tools")
local tool = script.Parent.Parent.ItemName
local player = script.Parent.Parent.Parent.Parent.Parent.Parent

script.Parent.MouseButton1Click:connect(function()
	if player.leaderstats:FindFirstChild("Money").Value >= price.Value then 
		player.leaderstats:FindFirstChild("Money").Value = player.leaderstats:FindFirstChild("Money").Value - price.Value
		game.ReplicatedStorage.ShopBuy:FireServer(tool.Value)
	end
end)

2 answers

This will probably be the most basic way.

While you have the shop part down, so this should not really be that hard.

Firstly, we get your mouse button function:

script.Parent.MouseButton1Click:connect(function()

end

Then, it looks like you know how to find the player. There is a property inside player: > Backpack

So let us add a variable for the player's Backpack:

script.Parent.MouseButton1Click:connect(function()
local backpack = player.Backpack

end

And now simply we just have the script find the tool and see if there is already the same tool inside the player using an IF statement:

script.Parent.MouseButton1Click:connect(function()
local backpack = player.Backpack

if backpack:FindFirstChild("tool") == nil then --nil means that the tool does not exist yet
	game.ReplicatedStorage.ShopBuy:FireServer(tool.Value) -- Functions
else
	wait() -- Do nothing if there is something
end

end

Now you have to adjust this to fit your script. This is probably the most basic way. There may be some typos so I want to apologize for that.

You should use localscript for faster click response

local tool = script.Parent.Parent.ItemName
local player = game.Players.LocalPlayer--You can use this to find the player on LocalScripts

script.Parent.MouseButton1Click:connect(function()
    local item = game.ReplicatedStorage.ShopBuy:InvokeServer(tool.Value)
	if item then print(item) end--Will say if you bought the item, didnt have money, already have the item or the item doesnt exist, depending on the server script answer
end)

Then test check if the player have the item, have enough money and give the item with this server script, so it avoid hackers exploiting clientside variables. Note that you will have to add a NumberValue or an IntValue with the name 'Price' on each Tool you want to be selled:

local event = game.ReplicatedStorage.ShopBuy
local tools = game.ReplicatedStorage:WaitForChild("Tools")
function buy(player,itemname)
	local tool = tools:findFirstChild(itemname)
	if tool and tool:findFirstChild("Price") then--Checks if tool exists and have a price
		if not player.Backpack:findFirstChild(itemname) then--Checks if tool is on players backpack
			if tool.Price.Value <= player.leaderstats:findFirstChild("Money").Value then--Checks if player has money to buy it
				player.leaderstats.Money.Value = player.leaderstats.Money.Value-tool.Price.Value--Player pays
				local toolc = tool:clone()--Clone the tool
				toolc.Parent = player.Backpack--Give the cloned tool to player
				return "Bought"
			else return "Not enough money"
			end
		else return "Already have the item"
		end
	else return "Item doesn't exist or doesn't have a price"
	end
end
event.OnServerInvoke = buy

This discussion is preserved as it was. New answers, comments, and votes are closed.