The script does not work on a certain line

Hello, I’m trying to make a game about red light and green light, but I have a little problem.

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local function StartRedLightGreenLight()
	local Enabled = false
	task.spawn(function()
		while true do
			Enabled = false
			ReplicatedStorage.UpdateUI:FireAllClients({"DownText", "Text", "Green Light"}, {"DownText", "TextColor3", Color3.fromRGB(77, 255, 57)})
			task.wait(5)
			ReplicatedStorage.UpdateUI:FireAllClients({"DownText", "Text", "Red Light"}, {"DownText", "TextColor3", Color3.fromRGB(255, 0, 0)})
			task.wait(0.3)
			Enabled = true
			task.wait(5)
		end
	end)
	while true do
		if Enabled then
			for _, player in pairs(Players:GetPlayers()) do
				if player.Character then
					local Humanoid = player.Character:FindFirstChild("Humanoid")
					if Humanoid then
						if Humanoid.MoveDirection.Magnitude > 0 then -- This never continues even if you are moving.
							Humanoid.Health = Humanoid.MaxHealth
						end
					end
				end
			end
		end
		task.wait()
	end
end

StartRedLightGreenLight()
2 Likes

Can you tell us at which line does it errors?

1 Like

This line:

Hello this is a spoiler

1 Like

Your brand new code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local function StartRedLightGreenLight()
	local Enabled = false
	task.spawn(function()
		while true do
			Enabled = false
			ReplicatedStorage.UpdateUI:FireAllClients({"DownText", "Text", "Green Light"}, {"DownText", "TextColor3", Color3.fromRGB(77, 255, 57)})
			task.wait(5)
			ReplicatedStorage.UpdateUI:FireAllClients({"DownText", "Text", "Red Light"}, {"DownText", "TextColor3", Color3.fromRGB(255, 0, 0)})
			task.wait(0.3)
			Enabled = true
			task.wait(5)
		end
	end)
	while wait() do
		if Enabled then
			for _, player in pairs(Players:GetPlayers()) do
				local Character = player.Character
                if Character then
					local Humanoid = Character:FindFirstChild("Humanoid")
					if Humanoid then
						if Humanoid.MoveDirection.Magnitude > 0 then
							Humanoid.Health = Humanoid.MaxHealth
						end
					end
				end
			end
		end
	end
end

StartRedLightGreenLight()

Hi, sadly it still didn’t work. But it prints on the output.

Instead of using MoveDirection, why not use HumanoidStateTypes? You could check if they are Walking, Running or Jumping, or even Falling, that’s where the robot should kill them.

Hi, I tried to use that but the players don’t die. But I have added prints on that line that modifies the health of the player and prints it on the output, but does not kill the player even if they are moving.

Can I see your code? Would be better.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local function StartRedLightGreenLight()
	local Enabled = false
	task.spawn(function()
		while true do
			Enabled = false
			ReplicatedStorage.UpdateUI:FireAllClients({"DownText", "Text", "Green Light"}, {"DownText", "TextColor3", Color3.fromRGB(77, 255, 57)})
			task.wait(5)
			ReplicatedStorage.UpdateUI:FireAllClients({"DownText", "Text", "Red Light"}, {"DownText", "TextColor3", Color3.fromRGB(255, 0, 0)})
			task.wait(0.3)
			Enabled = true
			task.wait(5)
		end
	end)
	while wait() do
		if Enabled then
			for _, player in pairs(Players:GetPlayers()) do
				local Character = player.Character
				if Character then
					local Humanoid = Character:FindFirstChild("Humanoid")
					if Humanoid then
						if Humanoid:GetState() == Enum.HumanoidStateType.Running then
							Humanoid.Health = Humanoid.MaxHealth
						end
					end
				end
			end
		end
	end
end

StartRedLightGreenLight()

May I ask why you are giving them their MaxHealth if they are running?
Aren’t you supposed to set their health to 0 if they are running?

Before the error use print(Humanoid.MoveDirection.Magnitude) to see why it isn’t working.
If that errors just trying printing Humanoid.MoveDirection. Tell me the result.

You’re right that fixed the problem. Thanks for your time!