#task.wait only works once

11 messages · Page 1 of 1 (latest)

bitter agate
#
ReplicatedStorage.Events.RED.OnServerEvent:Connect(function(plr)
    RGLPart.Color = Color3.fromRGB(255, 0, 0)
    ReplicatedStorage.Events.RED:FireAllClients()
    db = true

    task.wait(1) -- This

    RunService.Heartbeat:Connect(function()
        for Index, Player in ipairs(Players:GetPlayers()) do
            local Character = Player.Character or Player.CharacterAdded:Wait()
            local Humanoid = Character.Humanoid or Character:WaitForChild("Humanoid")
            if db == true then
                if Player.Team == game.Teams.Player then
                    if touched == true then
                        if Humanoid.MoveDirection.Magnitude > 0 and Humanoid.Health > 0 then
                            Humanoid.Health = 0
                        end
                    else
                        print("Player not in game.")
                    end
                else
                    print("Player not detected.")
                end
            else
                break
            end
        end
    end)
end)

So when the light turns red there's a one second delay before it kills just so that the players can have time to react but it only works once and kills instantly the next time.
I'm pretty new to scripting so I might've made a basic mistake, thanks if you decide to help me out.

lapis loom
#
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

ReplicatedStorage.Events.RED.OnServerEvent:Connect(function(plr)
    
    RGLPart.Color = Color3.fromRGB(255, 0, 0)
    ReplicatedStorage.Events.RED:FireAllClients()
    
    local db = true
    
    task.wait(1)
    
    RunService.Heartbeat:Connect(function()
        if db then
            for _, Player in ipairs(Players:GetPlayers()) do
                local Character = Player.Character
                local Humanoid = Character and Character:FindFirstChild("Humanoid")
                
                if Player.Team == game.Teams.Player and Humanoid and Humanoid.Health > 0 then
                    if touched then
                        Humanoid.Health = 0
                    else
                        print("Player not touched.")
                    end
                else
                    print("Player not in game or not on correct team.")
                end
            end
            db = false
        else
            RunService.Heartbeat:Disconnect()
        end
    end)
end)
#

try that @bitter agate

bitter agate
lapis loom
#

then idk

bitter agate
#

btw db is when to detect if its red or green light

#

when its red light db is true

#

when its green light db is false

slim ferry
#

It works the first time but then db is true forever

slim ferry