I have a store where you walk up to it and purchase a tool. Currently when a person purchases an item, multiple tools clone in the backpack at a time.
How do I make it so that the code let's multiple people purchase a item but it doesn't clone in the backpack so many times?
Thank you in advance.
price = 5 --Change this to the price of the tool!
local debounce = false
function getPlayer(humanoid)
local players = game.Players:children()
for i = 1, #players do
if players[i].Character.Humanoid == humanoid then return players[i] end
end
return nil
end
function onTouch(part)
local human = part.Parent:findFirstChild("Humanoid")
if (human ~= nil) and debounce == false then
debounce = false
local player = getPlayer(human)
if (player == nil) then return end
user = game.Players:findFirstChild(human.Parent.Name)
local gold = user.leaderstats.Gold
if gold.Value >= (price) then
gold.Value = gold.Value - (price)
script.Parent.Parent.Sword:clone().Parent = player.Backpack
wait(2)
m.Parent = nil
else
m = Instance.new("Message")
m.Text = "Sorry, insufficient funds"
m.Parent = player
wait(3)
m.Parent = nil
end
wait(2)
debounce = false
end
end
script.Parent.Touched:connect(onTouch)
