Help me add a detail to my script

I have this script that instantly kills the player if the “RedLight” Text label is visible however i want someone to help me make an adjustment so that players have 1 second to stop, so AFTER the red light has been visible for 1 second THEN it should kill whoever moves (walks) /please help

Script:

local fireArea = workspace.RedLightGreenLight.HitParts:FindFirstChild("FireArea")
local fireAreaSize = fireArea.Size / 2
local redLightStatusEvent = game.ReplicatedStorage:WaitForChild("RedLightStatus")

-- Variable to track if RedLight is visible
local redLightVisible = false

-- Function to check player movement in FireArea
local function checkPlayerMovementInFireArea()
	game:GetService("RunService").Heartbeat:Connect(function()
		for _, player in pairs(game:GetService("Players"):GetPlayers()) do
			local char = player.Character
			if char then
				local hrp = char:FindFirstChild("HumanoidRootPart")
				local humanoid = char:FindFirstChild("Humanoid")
				if hrp and humanoid then
					local min, max = fireArea.Position - fireAreaSize, fireArea.Position + fireAreaSize
					if hrp.Position.X >= min.X and hrp.Position.X <= max.X and
						hrp.Position.Y >= min.Y and hrp.Position.Y <= max.Y and
						hrp.Position.Z >= min.Z and hrp.Position.Z <= max.Z and
						humanoid.MoveDirection.Magnitude > 0 and redLightVisible then
						wait(2)  -- Wait for 2 seconds before killing
						humanoid.Health = 0
					end
				end
			end
		end
	end)
end

-- Listen for changes in RedLight status from the client
redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
	redLightVisible = isVisible
	if redLightVisible then
		checkPlayerMovementInFireArea()
	end
end)

wait 1 second after the light turns red to start checking for movement

1 Like

where? where should i put it excatly?

1 Like

So you want to give the player 1 second extra time to stop after red light?

You’d need to have wait() before you check their movement

2 Likes

Can you help me add that? if you could add it to my script

1 Like

A quick guess … with tick()

if redLightVisible then
	if redLightStartTime == 0 then
		redLightStartTime = tick()
	elseif tick() - redLightStartTime >= 1 and humanoid.MoveDirection.Magnitude > 0 then
		humanoid.Health = 0
	end
end
2 Likes

Add a wait() before the if statement checking for movement because it already checks instantly then kills automatically after 2 seconds. It just delays their death not the check for their movement itself.

2 Likes

One recommendation is to disconnect the RunService function in the function “checkPlayerMovementInFireArea()” once its not needed since the function could be called again creating more and more unneccassary function calling and creating lag, unless if you want to keep the runservice function to keep firing (I have no idea what you want to do with it, but this is just a suggestion if you don’t want that kind of functionality)

And for your question I would put a task.wait(1) before the function checkPlayerMovementInFireArea(),

redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
	redLightVisible = isVisible
	if redLightVisible then
        task.wait(1) -- Waits one second before firing function
		checkPlayerMovementInFireArea()
	end
end)

This will wait 1 second before calling the function which checks for player movement after the red light has been visible.

3 Likes

Im confused where do i put this

Where excatly is that lol im sorry, could you just copy my script and add it in and resend it

local fireArea = workspace.RedLightGreenLight.HitParts:FindFirstChild("FireArea")
local fireAreaSize = fireArea.Size / 2
local redLightStatusEvent = game.ReplicatedStorage:WaitForChild("RedLightStatus")

-- Variable to track if RedLight is visible
local redLightVisible = false
local redLightStartTime = 0  -- Time when the red light becomes visible

-- Function to check player movement in FireArea
local function checkPlayerMovementInFireArea()
	game:GetService("RunService").Heartbeat:Connect(function()
		for _, player in pairs(game:GetService("Players"):GetPlayers()) do
			local char = player.Character
			if char then
				local hrp = char:FindFirstChild("HumanoidRootPart")
				local humanoid = char:FindFirstChild("Humanoid")
				if hrp and humanoid then
					local min, max = fireArea.Position - fireAreaSize, fireArea.Position + fireAreaSize
					local playerMoving = humanoid.MoveDirection.Magnitude > 0
					
					-- Check if the player is within the FireArea and the red light is visible
					if hrp.Position.X >= min.X and hrp.Position.X <= max.X and
						hrp.Position.Y >= min.Y and hrp.Position.Y <= max.Y and
						hrp.Position.Z >= min.Z and hrp.Position.Z <= max.Z and
						redLightVisible then
						
						-- Wait 1 second after the red light is visible
						if tick() - redLightStartTime >= 1 then
							-- If the player is moving after 1 second, kill them
							if playerMoving then
								humanoid.Health = 0
							end
						end
					end
				end
			end
		end
	end)
end

-- Listen for changes in RedLight status from the client
redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
	redLightVisible = isVisible
	if redLightVisible then
		redLightStartTime = tick()  -- Record the time when red light is visible
		checkPlayerMovementInFireArea()
	end
end)

  • redLightStartTime: This variable tracks the time when the red light becomes visible using tick() (which returns the current time in seconds).

  • 1-second delay: We check if 1 second has passed since the red light became visible (tick() - redLightStartTime >= 1).

  • Movement Check: If the player is moving within the fire area after 1 second, they are killed.

1 Like

It stopped working completely, help now i never die

didnt work well, its just the same

sorr, maybe this one will help:

local fireArea = workspace.RedLightGreenLight.HitParts:FindFirstChild("FireArea")
local fireAreaSize = fireArea.Size / 2
local redLightStatusEvent = game.ReplicatedStorage:WaitForChild("RedLightStatus")

-- Variable to track if RedLight is visible and when it was activated
local redLightVisible = false
local redLightStartTime = 0  -- Time when the red light becomes visible

-- Function to check player movement in FireArea
local function checkPlayerMovement()
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
        local char = player.Character
        if char then
            local hrp = char:FindFirstChild("HumanoidRootPart")
            local humanoid = char:FindFirstChild("Humanoid")
            if hrp and humanoid then
                local min, max = fireArea.Position - fireAreaSize, fireArea.Position + fireAreaSize
                local playerMoving = humanoid.MoveDirection.Magnitude > 0
                
                -- Check if the player is within the FireArea and the red light is visible
                if hrp.Position.X >= min.X and hrp.Position.X <= max.X and
                    hrp.Position.Y >= min.Y and hrp.Position.Y <= max.Y and
                    hrp.Position.Z >= min.Z and hrp.Position.Z <= max.Z and
                    redLightVisible then
                    
                    -- Wait 1 second after the red light becomes visible
                    if tick() - redLightStartTime >= 1 then
                        -- If the player is moving after 1 second, kill them
                        if playerMoving then
                            humanoid.Health = 0
                        end
                    end
                end
            end
        end
    end
end

-- Listen for changes in RedLight status from the client
redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
    redLightVisible = isVisible
    if redLightVisible then
        redLightStartTime = tick()  -- Record the time when red light is visible
    end
end)

-- Continuously check player movement on every frame
game:GetService("RunService").Heartbeat:Connect(checkPlayerMovement)

1 Like

Still stops killing me completely, nothing kills my player now so the script stopped working

uhmm, it’s bigger issue that I thought, I won’t do it, cuz I don’t know what how exactly it must looks like, so, good luck/

any other idea you have? :sob: or anything

Could you join my game for a moment and see if you have time, in studio just a quick check

i can’t say exactly cuz i don’t see it and other stuff, other scripts maybe or not correct stuff in some services or guis for instance, and I don’t what you want do exactly with this script for gameplay (how it must looks)
so sorry

  • i’m not a scripter, i just know basic things

1 Like

Could you take a look in studio?