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

I have this loop, however if the task.wait is like this… The loop won’t play at all.

		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
				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
				task.wait(0.1)
			end
		end)

But, if it’s like this the loop will play but will stall the rest of the script. I want to know why it doesn’t play if the task.wait() is placed inside of the task.spawn.

3 Likes

Do you have any errors appearing in the output?

1 Like

I think I’m having a similar problem, I’m spawning a loop with task.spawn() and I’m trying to wait with task.wait() in it but it doesn’t wait and it just goes too fast, it doesn’t error tho

1 Like

Why don’t you try Tweening the attribute instead?
Like this

local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")

local Tween = TweenService:Create(Lighting,TweenInfo.new(1),{EnvironmentDiffuseScale = 0})

Tween:Play()
2 Likes

I’m not OP, I also can’t use Tweens because I need something else, something that isn’t achievable with Tweens
(OP’s problem may be solved with Tweens tho)

2 Likes

Oops,
Why isn’t it achievable? Should make your own post if you need help

2 Likes

Basically I’m trying to make something that goes exponentially (which as far as I know isn’t achievable with Tweens) but I’m also having the same problem as OP (I think)

It doesn’t work with wait() either
(If you want to go ahead do it in DMs)

2 Likes

I wrote this, it increments 2 variables each second using different loops,
I tested it and it worked

local Number = 0
local Number2 = 0

local Increment = 1

local Count = false

local function IncrementNumbers()
	task.spawn(function()
		while Count do 
			task.wait(Increment)
			Number += 1
			print(Number.." AA ")
		end
	end)
	task.spawn(function()
		while Count do 
			task.wait(Increment)
			Number2 += 1
			print(Number2.." AA ")
		end
	end)
end

Count = true
IncrementNumbers()
2 Likes

It waits right? I’ll try that and I’ll tell you if it works for me

1 Like

Yes it uses task.wait(). In the code I wrote

task.wait(Increment)
Number2 += 1

I meant to write

task.wait(1)
Number2 += Increment

But I’m sure you get the idea anyway

1 Like

Try using :GetService() instead of game.Lighting. If I remember correctly you can only change its properties if you use GetService.

local Lighting = game:GetService("Lighting")
task.spawn(function()
	workspace.GlobalSounds.powerDown:Play()
	task.wait(1)
	for count = 1, 10 do
		Lighting.EnvironmentDiffuseScale -= 0.156
		Lighting.EnvironmentSpecularScale -= 0.156
		task.wait(0.1)
	end
end)
1 Like

You can change it using game.Lighting

1 Like

It doesn’t work for me, my code works like this;
Calls function
After it creates a loop with task.spawn() which doesn’t wait
After it creates another loop with the same method, and doesn’t wait either

I can’t give the code sorry

1 Like

I tested this myself, and it works just fine with this codeblock.

task.spawn(function()
   task.wait(1)
   for i = 1, 10 do
   	print(i)
   end
end)

I believe the issue is coming from you calling the Play method of that Sound instance. By the way, you can achieve the same thing using

task.delay(1, function()
   -- your code
end)
3 Likes

try using corountine instead of spawn, i tested it in roblox studio and it should work:

coroutine.resume(coroutine.create(function()
	game.Workspace.GlobalSounds.powerDown:Play()
	task.wait(1)
	for count = 1, 10 do 
		game.Lighting.EnvironmentDiffuseScale -= 0.156
		game.Lighting.EnvironmentSpecularScale -= 0.156
		task.wait(0.1)
	end
end))

note that you only need to loop 7 times instead of 10 in order to make them reach 0

2 Likes

Sorry for the very late response. No. No errors at all. It’s why I made the post.

1 Like

It’s not the sound instance. I tried your method, but same issue. I also tried without the :Play() but nothing…

1 Like

What’s strange is that the top code was working just fine about a week ago. Did roblox change something?

1 Like

This doesn’t work. Same exact problem. Remove the task.wait() and it works fine, though

1 Like

Still looking for possible solutions.

2 Likes