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

Multiple Tools Cloning in the Inventory at once?

Asked by 6 years ago

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) 

1 answer

Log in to vote
0
Answered by 6 years ago

It's putting a bit of swords in the users backpack because you didn't add a check, so it keeps making the user buy more than one sword simultaneously.

But since you want multiple people buying at the same time then it's a bit more complicated.

In order for player's to buy at once what I did was put a value into the player's character who bought the item then I made it check if that value is in the player's character (this prevents the user from buying multiple items at once) and I also added a script to delete the value after 1 second (you can pick the time it deletes the value) (we want to delete the value after the specified time so the user can buy the item again)

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 



local user = game.Players:findFirstChild(human.Parent.Name)

local gold = user.leaderstats.Gold

if gold.Value >= (price) then

local check = Instance.new("StringValue")
check.Name = "bought"
local deletescript = script.Parent.deletescript -- the script that deletes "bought" scroll down more on the answer for the code to the delete script 

if not player.Character:FindFirstChild("bought") then
    gold.Value = gold.Value - (price)
    script.Parent.Parent.Sword:clone().Parent = player.Backpack
    check:Clone().Parent = player.Character
    deletescript:Clone().Parent = player.Character
    wait(2)
    m.Parent = nil
end

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) 

Delete Script Code:

while wait() do
if script.Parent:FindFirstChild("bought") then
wait(1)
script.Parent.bought:Destroy()
script:Destroy()
end
end
Ad

Answer this question