Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by
RjsMc 48
4 years ago

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.

Ad
Log in to vote
0
Answered by
Dfzoz 489 Moderation Voter
4 years ago

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

Answer this question