Loop doesn't work if task.wait is before it

Try this code and send what you get in output. Since the problem is most likely in your code.

task.spawn(function()
	local now1 = tick()
	task.wait(1)
	print(tick()-now1)
	for i=1, 3 do
		local now2 = tick()
		task.wait(1)
		print(tick()-now2)
	end
end)
1 Like

Just a bunch of failed to load sound data errors.

1 Like
  1. Try to run just in CommandBar (Command Line)
  2. If it starts, then the problem is in the script (either it is turned off, or it does not work and you still have to throw off its code)


Another error

1 Like

image
and this

Throw off the code where this loop is launched

	game.Workspace.GlobalSounds.powerDown:Play()
	task.spawn(function()
		task.wait(1)
		for count = 1, 10 do
			game.Lighting.EnvironmentDiffuseScale -= 0.17
			game.Lighting.EnvironmentSpecularScale -= 0.17
			task.wait(0.1)
		end
	end)
	task.spawn(function()
		local now1 = tick()
		task.wait(1)
		print(tick()-now1)
		for i=1, 3 do
			local now2 = tick()
			task.wait(1)
			print(tick()-now2)
		end
	end)

Like this? Nothing’s in the output and it behaves as it did before.

Are you sure that task.wait() is not working and function starts?

Hi, I tested your script exactly how you sent it and on my end it works, and just to clarify, is your issue about the sound not playing, the lightning not changing, or the thread not running entirely?

My issue’s this.

		task.spawn(function()
			task.wait(1)
			for count = 1, 10 do
				generate.BrightnessChange(0.08)
				game.Lighting.EnvironmentDiffuseScale -= 0.17
				game.Lighting.EnvironmentSpecularScale -= 0.17
				task.wait(0.1)
			end
		end)

If you have the code like this above, the LOOP doesn’t run. The sound plays, but not the loop.

However, if it’s like this

		task.wait(1)
		task.spawn(function()
			for count = 1, 10 do
				generate.BrightnessChange(0.08)
				game.Lighting.EnvironmentDiffuseScale -= 0.17
				game.Lighting.EnvironmentSpecularScale -= 0.17
				task.wait(0.1)
			end
		end)

It works, but it stops the entire thread since… It’s not in a corountine anymore. Which is undesired, I want it to not stop the entire thread. But if i put the task.wait() inside of the spawn, it the loop doesn’t run.

Yes, I’m sure. If you put it inside of the spawned function, the loop won’t run. But if it’s outside of it, the wait doesn’t work.

Are you sure your custom module is not yielding the script? Can you send the function?

function generate.generateRoom()
	local randomRoom = generate.getRandom()
	local newRoom = randomRoom:Clone()
	local newDoor = generate.NewDoor(newRoom)
	generate.RoomNum += 1
	newRoom:PivotTo(generate.prevRoom.Exit.CFrame)
	generate.prevRoom = newRoom

	for i, v in pairs(newRoom:GetChildren()) do
		if v.Name == "Lights" then
			v.Shine.Parent = game.Workspace.GeneratedLights
		end
	end
	
	if not generate.LightSystem and generate.RoomNum == 3 then
		game.Workspace.GlobalSounds.powerDown:Play()
		task.spawn(function()
			task.wait(1)
			for count = 1, 10 do
				game.Lighting.EnvironmentDiffuseScale -= 0.17
				game.Lighting.EnvironmentSpecularScale -= 0.17
				task.wait(0.1)
			end
		end)
	elseif generate.RoomNum > 3 and not generate.LightSystem then
		generate.BrightnessChange(0)
	elseif generate.LightSystem and generate.RoomNum >= 30 then
		generate.BrightnessChange(0.007)
	end

	newRoom.Parent = workspace.GeneratedRooms
	script.Parent.NewRoomGenerated:Fire()
end

The problem you’re facing might be related to something else in your script. I’ve reviewed your code, and it should work as intended. There might be another issue causing your problem, such as:

  1. game.Workspace.GlobalSounds.powerDown is not a sound or does not exist.
  2. There’s another script changing game.Lighting.EnvironmentDiffuseScale or game.Lighting.EnvironmentSpecularScale at the same time.
  3. The changes in game.Lighting.EnvironmentDiffuseScale or game.Lighting.EnvironmentSpecularScale are not visible due to other game settings or because the changes are too small to notice.

To check if your loop is actually running, you could add print statements within the loop, such as:

task.spawn(function()
    game.Workspace.GlobalSounds.powerDown:Play()
    task.wait(1)
    for count = 1, 10 do
        game.Lighting.EnvironmentDiffuseScale -= 0.156
        game.Lighting.EnvironmentSpecularScale -= 0.156
        print("In first loop, count = "..count) -- add this
        task.wait(0.1)
    end
end)

game.Workspace.GlobalSounds.powerDown:Play()
task.wait(1)

task.spawn(function()
    for count = 1, 10 do
        game.Lighting.EnvironmentDiffuseScale -= 0.156
        game.Lighting.EnvironmentSpecularScale -= 0.156
        print("In second loop, count = "..count) -- add this
        task.wait(0.1)
    end
end)

1 Like

Already tried adding print statements. Loop isn’t running at all. However, there is another function changing the enviromentspecularscale at the same time. It’s subtracting its value, so that may be the issue. There aren’t any errors, so if powerDown didn’t exist, it would say so. Plus I know it exists.

It is possible to make something that goes exponentially with tweens, but you have to properly use TweenInfo.

I haven’t tested it out, but I think something like this should work:

local tweenInfo = TweenInfo.new(Time, Enum.EasingStyle.Exponential, Enum.EasingDirection.In, 0, false, 0)
1 Like

If you want the spawned function to run 1 second later why don’t you use task.delay() ?

I did change it, but the issue persists.

Still have absolutely no clue why it isn’t working…

1 Like

That’s peculiar, honestly can’t see why it wouldn’t work because testing shows the loop to run perfectly fine. Have you tried using task.delay? It’ll achieve the same thing, and in the mean-time you can figure out what’s causing the loop to not run.