How do I stop players from spamming with a gear giver?

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)
1 Like

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.

1 Like

Can I have an example? I’m not really familiar with backpack

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
1 Like

You inverted your debounce variable. Set it to true and then false.

Even though it’s unconventional, the script only runs when Debounce = true. It’s not a problem.

local Debounce = true

if Player and Debounce == true then
	Debounce = false
	wait(1)
	Debounce = true
end

Just check if the user already has the 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 and not Player:WaitForChild("Backpack"):FindFirstChild(Tool.Name) then
		Debounce = false
		Tool:Clone().Parent = Player:WaitForChild("Backpack")
		wait(1)
		Debounce = true
	end
end)
2 Likes