How to use wait() in for loops?

I am trying to make all the items in a table change color and after 1 second take damage, but I have no idea how to make it do all at the same time.
For example (v is a character):

for i, v in pairs(targets) do
	v.Torso.Color = Color3.FromRGB(255, 255, 255)
	wait(1)
	v.Humanoid:TakeDamage(50)
end

It goes like that: Char A torso becomes white, and after a second he takes damage. Right after that Char B torso becomes white, and then he takes damage etc. But I want to make all the chars torso become white at the same moment and after 1 second take damage at the same moment. I know it’s not the best explaination. I heard that I should use task.wait(1) instead of wait(1), but it has the same effect.

You should always use task.wait over wait as it has tons of performance upgrades and doesn’t have much chances of infinite yielding if an error occurs in your script!

2 Likes
for i, v in pairs(targets) do
	v.Torso.Color = Color3.FromRGB(255, 255, 255)
	wait(1)
	v.Humanoid:TakeDamage(50)
end

issue with this is it wait for each loop cuz that how loop work, u can add task.spawn n for skip


for i, v in pairs(targets) do
	task.spawn(function()
		v.Torso.Color = Color3.FromRGB(255, 255, 255)
		wait(1)
		v.Humanoid:TakeDamage(50)
	end)
end
2 Likes

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