I want to make item, buyable from the button, that gives you only one tool, that you must to buy for money, and after tool has appeared in your inventory, you can't buy another same tool per your server rejoining.
Code: local Item = script.Parent.Item --Gun local Price = script.Parent.Price -- 200 local Currency = script.Parent.Currency --Money local Debounce = true
script.Parent.Touched:Connect(function(Hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent) if Player and Debounce == true and not Player:WaitForChild("Backpack"):FindFirstChild(Item.Name) then Debounce = false script.Connect(function(player) local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value) local ItemName = game.ReplicatedStorage:FindFirstChild(Item.Value) if CurrencyName.Value >=Price.Value then CurrencyName.Value = CurrencyName.Value - Price.Value ItemName:Clone().Parent = player.StarterGear ItemName:Clone().Parent = player.Backpack
end end) end end) .
Firstly, let me make that script more clear. Then, script.Connect
part looks kinda useless. Notice how you put .Value
at player.leaderstats:FindFirstChild(Currency.Value)
and then assign a .Value
again CurrencyName.Value
. Same with Item
. You also create ItemName
when you already have local Item
. And you should check if player has a gun equipped right now else he's gonna be able to have 2 guns at the same time. I suppose it should be looking like this:
local Item = script.Parent.Gun local Price = script.Parent.Price local Debounce = false script.Parent.Touched:Connect(function(Hit) local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent) if Player and Debounce == false and not Player.Backpack:FindFirstChild(Item.Name) and not Hit.Parent:FindFirstChild(Item.Name) then -- checks for gun in player's hands as well Debounce = true local playerCurrency = Player.leaderstats:FindFirstChild("Currency") if playerCurrency.Value >= Price.Value then playerCurrency.Value = playerCurrency.Value - Price.Value Item:Clone().Parent = Player.Backpack Debounce = false end end end)