Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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)
0
I can't see anything immediately wrong with the debounce in this. Could you explain what doesn't work about it? darkelementallord 686 — 4y
0
It doesn't add any delay SamuelaNutella 14 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Answer this question