Very Specific delay in task.spawn

You can write your topic however you want, but you need to answer these questions:

I am making a traffic light algorithm for a set of lights at an intersection in my game

For some reason the thread is running 4.5 seconds after all the other ones start, which doesnt make sense because the wait(4.5) is inside the function because the yellow light as a delay

Literally tried everything to solve, for some reason when i put 1 of the turning lights lower in the task.spawn(s) it runs perfectly but when the other one gets near it they both get a delay

This is the calling script


local TrafficModule = require(script.Parent.TrafficModule)

local function TurnOntoPrincesHwyNorthBound()
	-- Change signals
	
	task.spawn(function()		
		TrafficModule.GoRedLight(TrafficModule.RightTurnAndSingleLights.RightTurnAndSingleLight2, true, true)
		
		task.wait(4.5)
		TrafficModule.GoGreenLight(TrafficModule.SingleLights.Single3, false)
		TrafficModule.GoGreenArrow(TrafficModule.LeftTurnAndSingleLights.LeftTurnAndSingleLight1, false)
	end)

	task.spawn(function()
		TrafficModule.GoRedLight(TrafficModule.RightTurnAndSingleLights.RightTurnAndSingleLight4, true, true)
		
		task.wait(4.5)
		TrafficModule.GoGreenLight(TrafficModule.SingleLights.Single5, false)
	end)
	task.spawn(function()
		print("I AM RUNNING")
		TrafficModule.GoRedArrow(TrafficModule.RightTurnLights.RightTurn1)
	end)

	task.spawn(function()
		TrafficModule.GoRedLight(TrafficModule.SingleLights.Single2, false, false)
		
		task.wait(4.5)
		TrafficModule.GoGreenLight(TrafficModule.LeftTurnAndSingleLights.LeftTurnAndSingleLight2, true)
	end)

	task.spawn(function()
		TrafficModule.GoRedLight(TrafficModule.SingleLights.Single1, false, false)
		
		task.wait(4.5)
		TrafficModule.GoGreenLight(TrafficModule.LeftTurnAndSingleLights.LeftTurnAndSingleLight4, true)
	end)
	
	task.spawn(function()
		TrafficModule.GoRedLight(TrafficModule.SingleLights.Single6, false, false)
	end)
	
	task.spawn(function()
		TrafficModule.GoRedLight(TrafficModule.RightTurnAndSingleLights.RightTurnAndSingleLight1)
		TrafficModule.GoRedArrow(TrafficModule.RightTurnLights.RightTurn2)
	end)
	task.spawn(function()
		TrafficModule.GoRedLight(TrafficModule.RightTurnAndSingleLights.RightTurnAndSingleLight3, true, true)
	end)
	
	task.spawn(function()
		TrafficModule.GoRedLight(TrafficModule.RightTurnAndSingleLights.RightTurnAndSingleLight3, true, true)
	end)
	
	task.spawn(function()
		TrafficModule.GoRedLight(TrafficModule.LeftTurnAndSingleLights.LeftTurnAndSingleLight1, false, false)
	end)

end

This is the function inside the module script

function TrafficFunctions.GoRedArrow(Light)
	-- Turn on Yellow Arrow
	print("Turning on Yellow Arrow")

	Light.YellowArrow.Transparency = 0
	Light.YellowArrow.PointLight.Brightness = 1
	Light.YellowArrow.Material = Enum.Material.Neon

	wait(4.5)

	-- Turn off Yellow Arrow
	print("Turning off Yellow Arrow, turning on Red Arrow")

	Light.YellowArrow.Transparency = 0.8
	Light.YellowArrow.PointLight.Brightness = 0
	Light.YellowArrow.Material = Enum.Material.SmoothPlastic

	-- Turn on Red Arrow
	Light.RedArrow.Transparency = 0
	Light.RedArrow.PointLight.Brightness = 1
	Light.RedArrow.Material = Enum.Material.Neon
end

I’m losing my mind I need help please

Can you please clarify which section of the code is causing the issue?
I’m a bit confused on where you are experiencing the error, and which thing is running later than expected

1 Like

The First GoRedArrow call

task.spawn(function()
	print("I AM RUNNING")
	TrafficModule.GoRedArrow(TrafficModule.RightTurnLights.RightTurn1)
end)
1 Like

i mean maybe i dont quite understand the problem but does task.spawn yield? surely its more optimal here to use task.delay(4 or whatever the number is, function() ur code here end) or optionally using coroutine.wrap(function() ur code here with the task.wait() delays end)(), that way the tasks cant interfere with each other

For the purposes of OP’s script, task.spawn achieves the same thing as coroutine.wrap (they both cause yielding code to have no impact on things outside of the task/coroutine

with this sytem i use the wait 4.5 because lights should go green after the red function is done, so then there isnt green lights when others are going red yk, its a very confusing system but there is a pattern

im pretty sure multiple task.spawns() cause interfere when run in parralel like that, its best to use coroutines themselves

i guess also changing wait(4.5) to a task.wait like the others

I can’t seem to reproduce the issue you are facing, despite using the exact code that you provided initially.

Are you testing it in game or in studio?
Are you able to provide a video showing the issue, or even a place file (containing only the relevent things (so the script and any modules that are called, plus the traffic lights or just some placeholder lights)).

im gonna upload a video here just gimme a sec ill do it through youtube

heres the video

Do note that task.spawn depends on the amount of cores your CPU has. If you have less cores then multiple of these threads will stack up on one core which is most definitely the reason your code won’t work.

I suggest using task.delay.

how would i use task.delay in this scenario?

You replace your task.wait() with task.delay() and have all the code listed after the task.wait() inside of the function parameter in task.delay().

Also, in the video you provided, there is no bug? The “TrafficModule.GoRedArrow” function works just as the game starts?

If you isolate line 46, such that it is in its own task.spawn, does it work as expected? (same with line 12)

@lewisjxiv

the bug is that one of the arrow lights is going out of sync

Oh. I’ll just suggest using task.delay for this then.

im just gonna try and combine it all into one MASSIVE function and see if that works

I did a different structure and it works properly, even though it was a tricky one and doing it this way didnt work, thank you guys for trying to help me :slight_smile:

That is not correct, Lua(u) is single threaded and breaks threads up into smaller tasks that are done sequentially on a single CPU core. Task.spawn works identically to coroutines on the frontend.

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