How to force for i,v to start a function but not wait for it to finish and keep looping?

Basically the title, I have this script and alot of light models and I want them to flicker together but I can’t get them to do that even with coroutine, any ideas?

local Detector = script.Parent

local debounce = false

local function PersonalLightFlickerOffFunc(LightPart)
	
	repeat
		LightPart.Material = "Metal"
		LightPart.PointLight.Enabled = false
		LightPart.SpotLight.Enabled = false
		task.wait(0.1)
		LightPart.Material = "Neon"
		LightPart.PointLight.Enabled = true
		LightPart.SpotLight.Enabled = true
		task.wait(0.3)
	until
	game.SoundService.light_flicker_04.Ended
	
	LightPart.Material = "Metal"
	LightPart.PointLight.Enabled = false
	LightPart.SpotLight.Enabled = false
	LightPart["Buzzing lights"]:Stop()
	
end

local function PersonalLightFlickerOnFunc(LightPart)

	repeat
		LightPart.Material = "Neon"
		LightPart.PointLight.Enabled = true
		LightPart.SpotLight.Enabled = true
		task.wait(0.1)
		LightPart.Material = "Metal"
		LightPart.PointLight.Enabled = false
		LightPart.SpotLight.Enabled = false
		task.wait(0.3)
	until
	game.SoundService.light_flicker_04.Ended

	LightPart.Material = "Neon"
	LightPart.PointLight.Enabled = true
	LightPart.SpotLight.Enabled = true
	LightPart["Buzzing lights"]:Play()

end

Detector.Touched:Connect(function()
	
	if debounce == false then
		
		debounce = true
		
		game.SoundService.light_flicker_04:Play()
		
		for i,v in pairs(script.Parent.Parent.Parent:GetChildren()) do
			
			if v.Name == "FunctionalLight" then
				
				local MainLight = v.Light
				
				coroutine.wrap(PersonalLightFlickerOffFunc(MainLight))()
				
			end
			
		end
		
		game.SoundService.ScarySoundsFolder["Metal Groans 1 (SFX)"]:Play()
		wait(game.SoundService.ScarySoundsFolder["Metal Groans 1 (SFX)"].TimeLength-5)
		
		game.SoundService.light_flicker_04:Play()
		
		for i,v in pairs(script.Parent.Parent.Parent:GetChildren()) do

			if v.Name == "FunctionalLight" then

				local MainLight = v.Light

				coroutine.wrap(PersonalLightFlickerOnFunc(MainLight))()

			end

		end
		
	end
	
end)
1 Like

You can try task.spawn, it will wrap it in its own little house that will call it instantly and not wait for anything else to run.

2 Likes

It says invalid argument #1 to ‘spawn’ (function or thread expected)

task.spawn(PersonalLightFlickerOffFunc(MainLight))

try this

task.spawn(function()
  PersonalLightFlickerOffFunc(MainLight)
end)
1 Like

Well it works but not everyone one of them flickers some are left on, I tried checking them for any duplicates of anything but there isn’t anything wrong, even in the output

That is a separate issue, please mark the solution and make a new request if you need help fixing something else with the code.

1 Like

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