Wait() only working once

I’m making a simple red light green light game but the wait() only works once for some reason.

  1. What do you want to achieve?
    When the light is red I want to give a 1 second delay until it checks for movement just so that the player’s can have enough time to react.
  2. What is the issue?
    When I turn the light red for the first time the 1s delay works, but the second time there’s no delay at all.
    Roblox Studio Trimmed Clip 1 - Clipped with Medal.tv
  3. What solutions have you tried so far?
    I’ve looked on the forums, couldn’t find a problem similar to mine. I’ve asked it in a discord server I didn’t get answers.

I’m a pretty new scripter so I might’ve missed out on some basic mistakes

local detector = game.Workspace.NewMap.RGL.Detector
local touched = false

detector.Touched:Connect(function()
	touched = true
end)
detector.TouchEnded:Connect(function()
	touched = false
end)

local RGLPart = game.Workspace.NewMap.RGL.Color
local db = false

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

	wait(1)

	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)

Thank you if you decide to help me out!

You never disconnected the RunService.HeartBeat connection. To do this you just have to do. Break ends the loop in only that heartbeat. But once you set a redlight again, db is true and the code runs. Check the comments for edits.

ReplicatedStorage.Events.RED.OnServerEvent:Connect(function(plr)
	RGLPart.Color = Color3.fromRGB(255, 0, 0)
	ReplicatedStorage.Events.RED:FireAllClients()
	local heartbeatConnect -- Creating variable for connection.
	db = true

	wait(1)

	heartbeatConnect = RunService.Heartbeat:Connect(function() -- Setting connection to variable.
		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
				heartbeatConnect:Disconnect() -- Disconnecting the connection once a green light has been set again
			end
		end
	end)
end)


When I tried your script it just gave me this error, I don’t know why.

Remove break from the script and the error should be gone, if break runs first disconnect wont run.

Ohhh, okay. Thank you a lot, I learned that you have to disconnect the runservice.heartbeat

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.