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