Help me add a small easy detail to this script, No solution yet!

So you can see what im trying to do with this script, i just want help on how to add something to it so that the players have 1 seconds to stop walking/moving after the “RedLight” text is set on visible, THEN they get killed if they move

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)

Ive tried to get help from AI but i dont have chatgpt pro so i get bad answers lol and nothing worked

Hello!

Remove the wait(2) and add a task.wait(1) before the if redLightVisible statement.

So like this:


redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
    redLightVisible = isVisible
    task.wait(1)
    if redLightVisible then
        checkPlayerMovementInFireArea()
    end
end)

1 Like

So i did this and it still kills me instantly if i move the slightest when the RedLight text is set to Visible,

I want it to be so that the function that kills the player when its visible works after the RedLight text has been visible for at least 1 second, do you understand?

What they stated is the true solution. That wait will make it so you only check for movement at the right time and kill them shortly after. You may need to increase that wait time to see it take effect. Though once the first time happens since you never disconnect the heartbeat it will be instant afterwards. To fix that either make it disconnect that heartbeat after it’s done, or record the tick() that the light changed and add a check that tick() - redStartTick > 1 to handle the delay.

As a note though, there will be latency. The server doesn’t instantly know what speed the player is going and the time it takes to travel is pretty much ignored. So the path goes (server changes light → player gets light color; player sees; player stops walking → server sees player stopped walking). That means there is latency->reaction time->latency. So if the latency is 3/10s of a second that would lose you 6/10ths of your second for the player to respond just from how long it takes to replicate information. Leaving the player with 4/10ths a second to respond. And these times are not consistent.

Ohh, also you don’t want that heartbeat to connect multiple times so I recommend disconnecting it anyways just for simplicity. Either that or have it always run and not connect in that function.

try to place a wait infront of the long if statement

1 Like

don’t know how lol, maybe ill get smarter in the future

Still kills me instantly if i move the slighest during when the RedLight is visible, I want the function to kill people if they move ( IF THEY MOVE AFTER 1 SECOND OF WHEN ITS BEEN VISIBLE ALREADY ) do you see what i mean? thx for ur help

That does that? I don’t really get what you mean.

I meant thats what i want to happen and i tried what u said but it didnt work out, As you can tell theres a text that says red light and i want players to have a second to realize and stop if you know what i mean now, ill be really happy if you find the solution : D

Basically just remove the checkPlayerMovement function. Like keep what’s inside, but don’t put it in a function. Once you do that the first responders script will work (just remove the function call)

So like

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

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)

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

didnt do anything i have the same problem, its even worse now i get killed in a weird way when i move when the green light text is visible

create a new value that is called “KillCheckDelay” and set it to 1

in redLightStatusEvent behind redLightVisible if statement and BEFORE checkPlayerMovementInFireArea() enter the KillCheckDelay.

Code below:

local KillCheckDelay = 1 -- NEW

--code...

redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
    redLightVisible = isVisible
    if redLightVisible then
        wait(KillCheckDelay) -- NEW
        checkPlayerMovementInFireArea()
    end
end)

I also recommend adding a function that would stop “checkPlayerMovementInFireArea()” so next time it becomes RedLight the script wouldnt already be running. just do…

local KillCheckDelay = 1
local redLightVisible = false
local function checkPlayerMovementInFireArea()
    game:GetService("RunService").Heartbeat:Connect(function()
        if redLightVisible then -- NEW
            --code
        else
           return
        end -- /NEW
    end)
end

This here isn’t going to do that as they have already triggered it … this will just wait 2 seconds and then kill them …

You would want to stall that before it even has a chance to check … Right on this line;
if redLightVisible then task.wait(1) ← or 2

Also Heartbeat most likely is way too fast in this case you should be fine with Stepped checking 60 times a second.

After really looking over this … It seems like your script may go into an endless loop… This may work.
I think this should cover everything, even a log out in the middle of that loop.

local fireArea = workspace.RedLightGreenLight.HitParts:FindFirstChild("FireArea")
local redLightStatusEvent = game.ReplicatedStorage:WaitForChild("RedLightStatus")
local runService = game:GetService("RunService")
local fireAreaSize = fireArea.Size / 2
local redLightVisible = false
local char=nil

local function isPlayerInFireArea(hrp)
	local pos = hrp.Position
	local min, max = fireArea.Position - fireAreaSize, fireArea.Position + fireAreaSize
	return pos.X >= min.X and pos.X <= max.X and pos.Y >= min.Y and pos.Y <= max.Y and pos.Z >= min.Z and pos.Z <= max.Z
end

local function checkPlayers()
	while redLightVisible do 
		for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
			if player.Character ~= nil then char = player.Character
				local hrp = char and char:FindFirstChild("HumanoidRootPart")
				local humanoid = char and char:FindFirstChild("Humanoid")
				if isPlayerInFireArea(hrp) and humanoid.MoveDirection.Magnitude > 0 then
					humanoid.Health = 0
				end
			end
		end	runService.Stepped:Wait()
	end
end

redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
	redLightVisible = isVisible
	if redLightVisible then task.wait(1) --your 1 second stall
		checkPlayers()
	end
end)

Untested