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

How do i allow the players to buy only 1 of each item?

local player = game.Players.LocalPlayer
local leaderboard = player:WaitForChild("leaderstats")
local button = script.Parent
local price = button:WaitForChild("Price")
local item = button:WaitForChild("ItemName")
local rs = game:GetService("ReplicatedStorage")

button.MouseButton1Click:Connect(function()
	if leaderboard.Coins.Value>=price.Value then
		leaderboard.Coins.Value = leaderboard
		Coins.Value - price.Value
		local item = rs:WaitForChild(item.Value)
		item:Clone().Parent = player.StarterGear
		item:Clone().Parent = player.Backpack
	end
end)

while wait()do
	button.Text = item.Value.."-"..price.Value
end

I made the script above but i want to allow the players to buy only 1 of each item,(example: not like 10 bloxy cola but only allow them to buy 1 bloxy cola.) how do i do this?

2 answers

✓ Accepted answer

There are multiple ways on doing this, for 1 create a stock (how much is in stock) so how much they are selling then add if statement. For example if you added a IntValue or a NumberValue then define it then add this code;

local Stock = script.Parent:WaitForChild('IntValue') --Change this to the line or where IntValue is
local button = script.Parent

MouseButton1Down:Connect(function()
	if Stock.Value == 0 then
		return 
	else
		--Your code
	end
end)

also add a line so when item is purchased the stock goes down; Stock.Value = Stock.Value - 1

Though that means the player can buy a lot if the stock value is high. Another way is to create a variable for the button so that you would see how many time have the player bought this and what the limit is.

take this code as an EXAMPLE

local A = 0
local button = script.Parent

MouseButton1Down:Connect(function()
	if A == 1 then
		return
	else
		-- your code
	end
end)

make sure at the end of the code you add a line; A = A + 1 or A = 1

These are some easy ways to make sure there's a limit on how much a player can buy.

If this helped you find a idea or just helped then please accept answer ;)

BlackOrange3343,

Thanks alot for you answer, so if i am right this should be the code?

local A = 0
local button = script.Parent

MouseButton1Down:Connect(function()
    if A == 1 then
        return
    else
       local player = game.Players.LocalPlayer
local leaderboard = player:WaitForChild("leaderstats")
local button = script.Parent
local price = button:WaitForChild("Price")
local item = button:WaitForChild("ItemName")
local rs = game:GetService("ReplicatedStorage")

button.MouseButton1Click:Connect(function()
	if leaderboard.Coins.Value>=price.Value then
		leaderboard.Coins.Value = leaderboard
		Coins.Value - price.Value
		local item = rs:WaitForChild(item.Value)
		item:Clone().Parent = player.StarterGear
		item:Clone().Parent = player.Backpack
	end
end)

while wait()do
	button.Text = item.Value.."-"..price.Value
end 
    end
end)

1 comment

0

no rempve the top part from line 4 to 7 then add in a if statement on line 16, and say if a == 1 then return end, else if leaderboard blah blah blah and then at the end where u clone the item, do A = 1 or A = A + 1 (Please accept answer xD)

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