Question about a Laser script

I’ve coded a Laser script twice, the first time was using the delay function and the other one was using the tick function.
I wanted someone to tell me which one is going to have better performance since delay is almost the same thing as doing

spawn(function()
 wait(time)
 -- do stuff
end)

Here are the 2 methods:
First method (delay):

local Workspace = game:GetService("Workspace")
local World = Workspace:WaitForChild("World")
local Lasers = World.Lasers

local Hits = {}
local Connections = {}

local BaseDamage = 15
local DelayTime = 1.5

for _, Laser in next, Lasers:GetChildren() do
    Connections[Laser] = Laser.Touched:Connect(function(Hit)
        local Character = Hit.Parent
        if Character then
            local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
            if Humanoid then
                if Hits[Character.Name] then
                    return
                end
                
                Hits[Character.Name] = true
                Humanoid:TakeDamage(BaseDamage)
                
                delay(DelayTime, function()
                    Hits[Character.Name] = nil
                end)
            end
        end
    end)
end

Second method (tick):

local Workspace = game:GetService("Workspace")
local World = Workspace:WaitForChild("World")
local Lasers = World.Lasers

local Hits = {}
local Connections = {}

local BaseDamage = 15
local DelayTime = 1.5

for _, Laser in next, Lasers:GetChildren() do
    Connections[Laser] = Laser.Touched:Connect(function(Hit)
        local Character = Hit.Parent
        if Character then
            local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
            if Humanoid then
                if Hits[Character.Name] then
                    local TimeNow = tick()
                    if (TimeNow - Hits[Character.Name].LastDamaged) >= DelayTime then
                        Hits[Character.Name] = nil
                    else
                        return
                    end
                end
                
                Hits[Character.Name] = {
                    LastDamaged = tick()
                }
                Humanoid:TakeDamage(BaseDamage)
            end
        end
    end)
end

Both scripts work just the same

At this level, I don’t think there’s much of a difference, but I would probably say the second one would run more efficiently as you’re storing a single float and checking it as opposed to having the task scheduler handle a 5-second countdown just to set something to nil.

3 Likes