Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make a Debounce locally?

I'm making a tool giver, and I only want it to work once. It does this, however once one player touches it, nobody else can get the tool. In other words, how do I make a Debounce just for the Player and not the entire server? Here's the code:

local buttonPressed = false
 
script.Parent.Touched:connect(function(hit)
    if not buttonPressed and  hit.Parent:FindFirstChild("Humanoid")  then
	local plr = workspace[hit.Parent.Name]
	local player = game.Players:GetPlayerFromCharacter(plr)
   
 
    buttonPressed = true
   
	local tool = game.Lighting.Tools["Purple Glowstick"]:Clone()
	tool.Parent = player.Backpack


	plr.Humanoid.Died:Connect(function()
	tool:Destroy()
	wait(10)
	buttonPressed = false
	
	end)
	end
	end)


And yes, I've tried a LocalScript. The LocalScript made the function not even work for one player.

1 answer

✓ Accepted answer

Right now as you said you're using the debounce server side, which can be changed by any player. Instead of using a debounce within the code, you can check the players backpack and character for tools and only give them one if they don't already have one. If you would like to have multiple tools that you can only get one of, you'll have to have an additional check for the name of the tool the player is trying to receive. Here's a script I wrote applying this method for a single tool:

local players = game:GetService("Players")
local Lighting = game:GetService("Lighting")
local part = script.Parent
local toolName = "Purple Glowstick" -- Set the name of the tool that this giver gives

part.Touched:Connect(function(hit)
	if players:FindFirstChild(hit.Parent.Name) then
		local player = players:FindFirstChild(hit.Parent.Name)
		local backpackObjects = player.Backpack:GetChildren()
		local characterObjects = player.Character:GetChildren()
		for i = 1, #backpackObjects do
			if backpackObjects[i]:IsA("Tool") and backpackObjects[i].Name == toolName then
				return
			end
		end
		for i = 1, #characterObjects do
			if characterObjects[i]:IsA("Tool") and characterObjects[i].Name == toolName then
				return
			end
		end
		local tool = Lighting.Tools[toolName]:Clone()
		tool.Parent = player.Backpack
	end
end)

If you have any questions or problems with this answer let me know. I'd be happy to clarify or fix it.

Hope this helps!

10 comments

0

Hmm, It appears that it doesn't work. I don't know if this matters, but there will be multiple tool givers and not just one. Thanks for trying though :)

0

Did you get any errors?

0

No errors, nothing at all.

0

Can you send me a picture of your explorer?

0

I'm also going to make an edit to the script that works for multiple givers.

0

This script should go inside of the part that you want to give the tool when touched.

0

It works, but It's a bit laggy, anyway to fix that with the for 1 = i or whatever?

0

I'm not sure why it would be laggy, but the for loops check through the player's backpack and character for any tools and if their name is the same as the one given by that giver.

0

Maybe it was just studio acting up, Thanks man you are a life saver! :D

0

Happy to help.

This discussion is preserved as it was. New answers, comments, and votes are closed.