I have this script for giving gears when touching a part. However, the player can just stand on it and get 30 million of a gear. How can I limit them to one of each type?
local RS = game:GetService("ReplicatedStorage")
local Tool = RS:FindFirstChild("cart")
local Debounce = true
script.Parent.Touched:Connect(function(Hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
if Player and Debounce == true then
Debounce = false
Tool:Clone().Parent = Player:WaitForChild("Backpack")
wait(1)
Debounce = true
end
end)
Look for the specific tool in the player’s backpack (where tools are stored when unequipped) and character (where tools are stored when equipped) and see if they already own said tool.
local RS = game:GetService(“ReplicatedStorage”)
local Tool = RS:FindFirstChild(“cart”)
local Debounce = true
script.Parent.Touched:Connect(function(Hit)
local Player = game:GetService(“Players”):GetPlayerFromCharacter(Hit.Parent)
if Player and Debounce == true then
local IfTheyHaveItAlready = Player.Backpack:WaitForChild(Tool.Name) or Player.Character:WaitForChild(Tool.Name)
if IfTheyHaveItAlready ~= nil then print “already have it” return end
Debounce = false
Tool:Clone().Parent = Player:WaitForChild(“Backpack”)
wait(1)
Debounce = true
end
end)
Backpack is a container held in the player which contains all the tools a player is holding.
You can access it by Player.Backpack
In your situation, you might want to do something like this:
if Player and Debounce == true and not Player.Character:FindFirstChild(Tool.Name) and not Player.Backpack:FindFirstChild(Tool.Name) then
-- :FindFirstChild can also return a bool of whether or not an item with said name is the child of an object.
-- We use it here to look for the tool in both the backpack and the character.
Debounce = false
Tool:Clone().Parent = Player.Backpack
wait(1)
Debounce = true
end
local RS = game:GetService("ReplicatedStorage")
local Tool = RS:FindFirstChild("cart")
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(Tool.Name) then
Debounce = false
Tool:Clone().Parent = Player:WaitForChild("Backpack")
wait(1)
Debounce = true
end
end)