Problem with :GetChildren()) do

So basically i’m making a button that makes your entire character forcefield.
But what’s happening is that only 1 bodyPart gets forcefield every 5 seconds.
I want it to make every single one of them forcefield instantly.

-- Services

local RS = game:GetService("ReplicatedStorage")

------------------------------------------

-- Variables

local Remote = RS:WaitForChild("ToggleSkill")

local SkillDuration = 5

------------------------------------------

Remote.OnServerEvent:Connect(function(player)
	
	local character = player.Character
	
	local function TurnSkillOn(material, oldMaterial)
		for i, bodyPart in pairs(character:GetDescendants()) do
			if bodyPart:IsA("BasePart") or bodyPart:IsA("Part") or bodyPart:IsA("MeshPart") then
				bodyPart.Material = material
				wait(SkillDuration)
				bodyPart.Material = oldMaterial
			end
		end
	end
		
	TurnSkillOn("ForceField", "Plastic")
end)

This is because of this wait. You can wrap it in a task.spawn, or use coroutines.
try:

task.spawn(function()
   task.wait(SkillDuration)
end)

When using a for loop, and you want each time to be checked/included immediately, do not put a delay. Or wrap the delay inside a spawn function or coroutine.

2 Likes

I knew I could do this but since i’m dumb I forgot to.
Thanks a lot!

2 Likes