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

How can I add a .5 second delay to my localscript?

Hi. I'm trying to make a .5 second delay in my localscript. I've already tried to add debounce to my localscript but for some reason, it just doesn't work. Could someone explain what I'm doing wrong?

without debounce:

local tool = script.Parent
local player = game.Players.LocalPlayer
repeat wait() until player.Character ~= nil and workspace:FindFirstChild(player.Name) ~= nil
local character = player.Character
local root = character:WaitForChild("HumanoidRootPart")
 
tool.Equipped:Connect(function(mouse)
		mouse.Button1Down:Connect(function()
				local ray = Ray.new(root.CFrame.p, root.CFrame.lookVector * 4)
				local hit, hitposition = workspace:FindPartOnRay(ray, character)
				local humanoid = hit.Parent:FindFirstChild("Humanoid")
					
				if not humanoid then
					humanoid = hit.Parent:FindFirstChild("Humanoid")
				end
				if humanoid ~= nil and (root.Position - hitposition).magnitude <= 50 then
					humanoid:TakeDamage(30)
				end
		end)
end)

with debounce:

local tool = script.Parent
local player = game.Players.LocalPlayer
repeat wait() until player.Character ~= nil and workspace:FindFirstChild(player.Name) ~= nil
local character = player.Character
local root = character:WaitForChild("HumanoidRootPart")
local debounce = false
 
tool.Equipped:Connect(function(mouse)
			mouse.Button1Down:Connect(function()
				if debounce == false then
					debounce = true
					local ray = Ray.new(root.CFrame.p, root.CFrame.lookVector * 4)
					local hit, hitposition = workspace:FindPartOnRay(ray, character)
					local humanoid = hit.Parent:FindFirstChild("Humanoid")
						
					if not humanoid then
						humanoid = hit.Parent:FindFirstChild("Humanoid")
					end
					if humanoid ~= nil and (root.Position - hitposition).magnitude <= 50 then
						humanoid:TakeDamage(30)
					end
				end
			wait(.5)
			debounce = false
		end)
end)

2 comments

0

I can't see anything immediately wrong with the debounce in this. Could you explain what doesn't work about it?

0

It doesn't add any delay

1 answer

Hello again. I fixed it, here's the finished script:

local tool = script.Parent
local player = game.Players.LocalPlayer
repeat wait() until player.Character ~= nil and workspace:FindFirstChild(player.Name) ~= nil
local character = player.Character
local root = character:WaitForChild("HumanoidRootPart")
local debounce = false

tool.Equipped:Connect(function(mouse)
        mouse.Button1Down:Connect(function()
			if debounce == false then
				debounce = true
                local ray = Ray.new(root.CFrame.p, root.CFrame.lookVector * 4)
                local hit, hitposition = workspace:FindPartOnRay(ray, character)
				if hit then
					local humanoid = hit.Parent:FindFirstChild("Humanoid")
					
					if not humanoid then
                    	humanoid = hit.Parent:FindFirstChild("Humanoid")
                	end
                	if humanoid ~= nil and (root.Position - hitposition).magnitude <= 50 then
                   	 	humanoid:TakeDamage(30)
                	end
				end
				wait(.5)
				debounce = false
			end

               
        end)
end)

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