Tool giver gives me the tool too much times

Hi! I’m trying to make a tool giver that only gives you the tool once, but it gave me the tool more than once, help? Here’s the code!

script.Parent.Touched:Connect(function(hit)

local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

local tool = game.ReplicatedStorage.Guns.Grenade:Clone()

tool.Parent = plr.Backpack

wait (math.huge)

end)
1 Like

You need to do a check on the player if the player has the tool or not. This will prevent it from giving the player too many tools because the .Touched event fires multiple times per second.

The Touched event is fired many times once it is touched, implement a debounce.

local debounce = false
script.Parent.Touched:Connect(function(hit)
	if debounce then 
		return 
	end
	debounce = true
	
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	local tool = game.ReplicatedStorage.Guns.Grenade:Clone()
	tool.Parent = plr.Backpack
	
	wait(3)
	debounce = false
end)
2 Likes

Welp here we go again with Touched events

Just add a simple conditional check if the Player’s Backpack/Character has the tool or not

script.Parent.Touched:Connect(function(hit)
    local Char = hit.Parent
    local plr = game.Players:GetPlayerFromCharacter(Char)
    if plr then
        if plr.Backpack:FindFirstChild("Grenade") == nil and Char:FindFirstChild("Grenade") == nil then
            local tool = game.ReplicatedStorage.Guns.Grenade:Clone()
            tool.Parent = plr.Backpack
        end
    end
end)
3 Likes

`

local debounce = {}

script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if not plr then return end
if table.find(debounce, plr.UserId) then return end

local tool = game.ReplicatedStorage.Guns.Grenade:Clone()

table.insert(debounce, plr.UserId)
tool.Parent = plr.Backpack
wait(10)
table.remove(debounce, plr.UserId)
end)
1 Like

you should add a check if the player already has it, that way other players can also get it. here is an edited version:

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not plr then return end--Makes Sure What Touched It Really Is A Player
	if plr.Backpack:FindFirstChild("Grenade") then return end--Makes Sure The Player Doesn't Have A Grenade
	--
	local tool = game.ReplicatedStorage.Guns.Grenade:Clone()
	tool.Parent = plr.Backpack
end)
2 Likes

Thank you everyone! Here’s the game I used it for!

https://www.roblox.com/games/6703792405