Looking for someone who will join me in studio for a quick moment and check my problem / STILL NEED HELP

Hi, Its about a script but i think the issue might be in something else in the game, everything is organized and ive been on my pc for almost 3 days with this problem without a solution.

Ill be extremely grateful if someone could come it would probably take just 3 minutes max if you are good with scripting, The script isnt long at all, Im just really dumb sometimes.

Please reply if you think you could help, its a small detail i want to add to a script : D

1 Like

please reply if someone could help me, it wont take long thank you so much

what is an issue and what you need help with be more specific

1 Like

I have a red light green light game but my script kills the player instantly and doesnt even give them 0.1 seconds to think and stop when the “RedLight” textlabel is visible, Do you see what im trying to say? it sounds like a easy problem but no one has said something that worked, Could you visit me in studio for a moment?

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)
1 Like

the script says that the players die 2 seconds after they move if they move when the red light text is set to visible

2 Likes

The problem is you are checking if it is visible and THEN waiting two seconds and killing.

First, wait two seconds, THEN check if it is visible and proceed to kill.
Move the “wait(2)” before the if statement. Also, please use task.wait :sob:

edit - here’s what the code would look like w/ the change:

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
                    task.wait(2)  -- Wait for 2 seconds BEFORE checking and killing
					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
						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)
1 Like

i just got 4 errors one of them about replicated storage, im so sorry but if you could join me in studio you would help me out so much, can you do that

Could you show the errors in your output? I can’t imagine why that would happen lol

I can’t at the moment, but maybe later. Once I got some time I’ll try to test your code myself first, but in the meantime can you send a picture of your explorer? Specifically where your script is. Probably won’t help much, but maybe it’ll help with something.

the thing is that ive showed a lot of people and if you could, tell me when you have time to check, i dont think talking is gonna solve it. It would help me out so much if you could join me in studio for a short moment

Alright, if you insist then I guess I can take a look. Just know that a newborn child is probably more qualified to help :sob:

Sent you a friend request :+1:

1 Like

This code RUNS EVERY FRAME. Basically once red light is toggle on, killing is enabled and any further movement results in killing. Unless Red light is toggled off. But the issue is that it will work properly only once, and then instakill on next toggles. To solve this, Disconnect function. To do so, store it as variable:

local Connection

Connection = game:GetService("RunService").Heartbeat:Connect(function() ...)

redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
	redLightVisible = isVisible
	if redLightVisible then
		checkPlayerMovementInFireArea()
	elseif Connection then
		Connection:Disconnect()
		Connection = nil
	end
end)

In your case only possible issue with it is that you tried to index not existing object in Replicated Storage.

That’s a great observation which may be useful for solving the issue.

However, I don’t see why that’d create an issue. As long as the player is not moving after the initial run’s 2 second wait, why would there be errors and/or incorrect kills? Maybe the function has to be disconnected once redLightVisible is false, and then reconnected once needed? I haven’t programmed in LuaU in months, as I’ve solely been focusing on modelling and other skills, so I’m incredibly rusty at the moment; I probably sound stupid asf :sob:

This looks like it would work, but what about the next time the red light turns on? Wouldn’t the call of “checkPlayerMovementInFireArea()” be useless, as Connection is disconnected?

That’s what code will do. Ofc if there’s only 2 states of red light - toggled and not toggled. Otherwise this needed:

if redLightVisible == true then
	checkPlayerMovementInFireArea()
elseif redLightVisible == false and Connection then
	Connection:Disconnect()
	Connection = nil
end

Because connection will be connected twice+, or in other words - stacked.

1 Like

Managed to fix the code! Turns out, moving the wait before the if statement was part of the solution, however removing the function from its connection to RunService ultimately was what made it work.

Remove me from game permissions once you test the code and verify that it works, and unfriend me if you’d like @nonoli2007. Glad I was able to help you.

Solution (already in the script in studio)
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 Connection = 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
				task.wait(2)  -- Wait for 2 seconds before killing (note by F1ghtOn: I reccomend reducing this, as it gives a LOT of wait time)
				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
					humanoid.Health = 0
				end
			end
		end
	end
end

redLightStatusEvent.OnServerEvent:Connect(function(_, isVisible)
	redLightVisible = isVisible
	if redLightVisible then
		Connection()
	end
end)

-- old function code
--[[ 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
					task.wait(2)  -- Wait for 2 seconds before killing
					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
						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) --]]