Make loop run on multiple players at once

So here I have a loop which spawn 6 blocks on top of a player inside a loop containing all players, but as you can see it switches from one player to the other obviously. I wanna make both loops run on both players at the same time (server sided), does anyone know how to go about this? (Wildly alternative code is appreciated too.)

function dropBlock()
	findHRP() -- Basically just finds all HumanoidRootParts in-game, so the game can actively get their current positions every time loop runs by
	
	if #ActiveRootTable == 0 then return end
	
	isRunning = true
	
	for _,HumanoidRootPart in pairs(ActiveRootTable) do
		
		for i = 0,5,1 do
			
			local block = Instance.new("Part")
			
			block.Position = HumanoidRootPart.Position + Vector3.new(0,8,0)
			block.Parent = workspace
			debris:AddItem(block, 3)
			
			task.wait(1)
		end
	end
	
	isRunning = false
end


while task.wait(1) do
	if isRunning == false then
		dropBlock()
	end
end
2 Likes

There are 2 solutions to this, you can wrap the for i = 0,5,1 loop inside of a coroutine.wrap() or you can instead put the for _,HumanoidRootPart in pairs(ActiveRootTable) loop inside of the for i = 0,5,1 loop

For the first solution which is wrap the for i = 0,5,1 loop inside of a coroutine.wrap() you can replace this code

for i = 0,5,1 do
			
	local block = Instance.new("Part")
			
	block.Position = HumanoidRootPart.Position + Vector3.new(0,8,0)
	block.Parent = workspace
	debris:AddItem(block, 3)
			
	task.wait(1)
end

with this code

coroutine.wrap(function()
	for i = 0,5,1 do
			
		local block = Instance.new("Part")
			
		block.Position = HumanoidRootPart.Position + Vector3.new(0,8,0)
		block.Parent = workspace
		debris:AddItem(block, 3)
			
		task.wait(1)
	end
end)()

And for the second solution which is put the for _,HumanoidRootPart in pairs(ActiveRootTable) loop inside of the for i = 0,5,1 loop you can replace this code

for _,HumanoidRootPart in pairs(ActiveRootTable) do
		
	for i = 0,5,1 do
			
		local block = Instance.new("Part")
			
		block.Position = HumanoidRootPart.Position + Vector3.new(0,8,0)
		block.Parent = workspace
		debris:AddItem(block, 3)
			
		task.wait(1)
	end
end

With this code

for i = 0,5,1 do
	for _,HumanoidRootPart in pairs(ActiveRootTable) do
		local block = Instance.new("Part")
			
		block.Position = HumanoidRootPart.Position + Vector3.new(0,8,0)
		block.Parent = workspace
		debris:AddItem(block, 3)
	end

	task.wait(1)
end

use loop through players, then corountine that have invidual loop inside, it vvil help

1 Like

Thank you man!

Iā€™d like to note that I forgot to add table.clear in the original post at the end of the function and I also added a task.wait(5) after the loop just so that it waits until all loops are done.

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