Debounce for one client only?

  1. What do you want to achieve?
    I have a script for when the player touches the part, they get a tool. But I want it to give them one tool, and I thought I could do that with a debounce.

  2. What is the issue?
    The debounce happens for everyone, so no other player except for the first one to touch the part will get the tool. I know I could put a wait, but I think it would be better if multiple people can get it at once, like if someone is showing someone else how to get it. It’s kind of hard to get.

  3. What solutions have you tried so far?
    I tried to use a local script, but that failed.

This is my first post, and I am also still pretty new to scripting, so any help would be appreciated!

Hmm I have an idea which might work
script in localscript:

local plr = game:GetService("Players").LocalPlayer
local toolGiver = game.Workspace.ToolGiver
local debounce = false
local toolRemote = game.ReplicatedStorage.RemoteEvent

toolGiver.Touched:Connect(funtion()
 --code that detects if the localplayer touched the part
 if localPlayerTouched  then
  if debounce == false then
   debounce = true
   toolRemote:FireServer()
  end
 end
end

code inside script in a serverscriptservice:

local toolRemote = game.ReplicatedStorage.RemoteEvent
toolRemote.OnServerEvent;Connect(function()
 --code that gives the tool to the player
end

This method is not exploit-proof as an exploiter might use a localscript to fire the event more than once and get another tool.
If this doesn’t work, can you show the code you are using inside your script(s)? Then i could tell you if there is something wrong with it.

Do a normal touched event with debounce but instead of only 1 debounce, make a table. Everytime a player enter the game, create a new debounce for them.

I would say that you don’t even need debounce for this
A simple check to see if the player already has the tool is all that is needed

if not(player.Backpack:FindFirstChild("Tool Name Here")) then
   -- Give player the tool
end

Generally the purpose of debounce is to make it where a player can’t trigger something in rapid succession, like pressing a button 9999 times in 1 second

local Debounce = {};

local function AddToDebounce(Plr)
  Debounce[Plr.Name]=true;
        delay(1,function()
            Debounce[Plr.Name]=nil;
        end);
end;

--//
local function OnTouchEvent(Plr)
    if(not Debounce[Plr.Name])then
        AddToDebounce(Plr)
        --<Code
    end;
end;
2 Likes

I tried putting that into the script with some help from the wiki, and this is what I came up with:


-- give tool function
local function giveTool(player, tool)
	local backpack = player:FindFirstChildOfClass("Backpack")
	
	if not(backpack:FindFirstChild("Ultimate weapon")) then
		local toolClone = tool:Clone()
		toolClone.Parent = backpack
	end
end

-- will give the tool when player touched block
script.Parent.Touched:Connect(function(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	
	if human then
		giveTool()
	end
end) ```

But now I am getting a "attempt to index nil with 'FindFirstChildOfClass'".

Try backpack = player:FindFirstChild(“Backpack”) instead

It seems like anything that I put after player gets the same error, attempt to index nil with ___.

Ah when you fire

giveTool()

You need to put in the parameters

giveTool(game.Players:GetPlayerFromCharacter(hit.Parent), game.ReplicatedStorage["Ultimate Weapon"])

Change the 2nd parameter to where your weapon is

1 Like

Try this maybe

local toolGiver=game.Workspace.ToolGiver
local tool=game.ReplicatedStorage.Tool --change the path to your tool

toolGiver.Touched:connect(function(t)
if game.Players:findFirstChild(t.Parent.Name) then
local plr=game.Players:findFirstChild(t.Parent.Name)
if not plr.Backpack:findFirstChild(tool.Name) and not plr.Character:findFirstChild(tool.Name) then
tool:Clone().Parent=plr.Backpack
end
end
end)

That is super duper insecure…

@uopwq1
Instead, do something like this:

local Debounce = {}

part.Touched:Connect(function(hit)
   local player = game.Players:GetPlayerFromCharacter(hit.Parent)

   if player the 
       if Debounce[player.UserId] then
          return
       else
          Debounce[player.UserId] = true
          delay(3, function()
             Debounce[player.UserId] = nil
          end)
          local clone = tool:Clone()
          clone.Parent = player.Backpack
       end
    end
end)