I’m making a simple red light green light game but the wait() only works once for some reason.
-
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. -
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 -
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!
