Code grabs players one by one, individually

Hi, so uh, I’m making a code based on a magnitude. Everything works properly except for one things, a minor bug that is confusing me.
When the script detects 2 or more people, it will indivually add and finish the particles, then move on and so forth. I would think by grabbing all rootparts, that it would find the magnitude of all players, but it doesn’t… please tell me where the error lies.


local Players = game:GetService("Players")
local Tool = script.Parent
local Fire = workspace.Fire
local players = game:GetService("Players")
local RepilcatedSpontaneousCombustion = game:GetService("ReplicatedStorage"):WaitForChild("Spontaneous Combustion")
local MagicCooldown = game:GetService("ReplicatedStorage"):WaitForChild("Cooldown")


Players.PlayerAdded:Connect(function(player)

	player.CharacterAdded:Connect(function(character)
			local rootpart = character:WaitForChild("HumanoidRootPart")
			local humanoid = character:WaitForChild("Humanoid")
			local Backpack = player:WaitForChild("Backpack")
		wait(RepilcatedSpontaneousCombustion)
		local SpontaneousCombustion = RepilcatedSpontaneousCombustion:Clone()
		SpontaneousCombustion.Parent = player:WaitForChild("Backpack")
			SpontaneousCombustion.Equipped:Connect(function(equipped)
			for _,v in pairs(game.Players:GetPlayers()) do
				MagicCooldown:FireClient(player)
				local allroots = v.Character:FindFirstChild("HumanoidRootPart")
				print(_,v)
				if allroots ~= rootpart then
					if ((allroots.Position - rootpart.Position).Magnitude > 20) == false then
						print(allroots)
						allroots.ParticleEmitter.Enabled = true
						wait(5)
						allroots.ParticleEmitter.Enabled = false
					end
				end
			end
		end)
	end)
end)

You need to create a separate thread for each player if you want to make it run through all players simultaneously.


You can use task.spawn() for that:

Use task.spawn(function() to make the code run asynchronously.


The code wasn’t formatted in studio but it should work:

for _,v in pairs(game.Players:GetPlayers()) do
            task.spawn(function()
				MagicCooldown:FireClient(player)
				local allroots = v.Character:FindFirstChild("HumanoidRootPart")
				print(_,v)
				if allroots ~= rootpart then
					if ((allroots.Position - rootpart.Position).Magnitude > 20) == false then
						print(allroots)
						allroots.ParticleEmitter.Enabled = true
						wait(5)
						allroots.ParticleEmitter.Enabled = false
					end
				end
              end)
			end

Recommendations:

I also recommend using task.wait(5) instead of wait(5):

Also, you can replace your pairs with ipairs. ‘Indexed Pairs’ will run faster in comparison to Pairs:

The only situation I know of where ipairs doesn’t work is when you’re working with dictionaries. :GetPlayers() is an array so ipairs will work for you.


Hope this helps! :smile:

2 Likes

Thank you so much. I didn’t know task was even a thing. I only have 2 clients set up to my computer, my camrecorder and my computer, so I need a 3rd person to test this, but theoretically it should work, thank you. (Also I was using pairs because it didn’t matter about the order, but now that I know ipairs is faster, I’ll definetly use that!)

1 Like

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