How can you get the players HumanoidRootPart from a for i,v in pairs loop?

I’ve been working more on this script, but I’ve run into a problem. I can’t figure out how to get the Players HumanoidRootPart from a for i,v in pairs loop, how exactly can you achieve that?

local IntermisssionTime = 5
local RoundTime = 120 --Game ends when the time is over, or when someone gets 8 kills.
local Intermission = 0
local Round = 0
local Spawn1 = game.Workspace.Spawns.Spawn1
local Spawn2 = game.Workspace.Spawns.Spawn2
local Spawn3 = game.Workspace.Spawns.Spawn3
local Spawn4 = game.Workspace.Spawns.Spawn4
local Spawn5 = game.Workspace.Spawns.Spawn5
local Spawn6 = game.Workspace.Spawns.Spawn6
local SpawnSelected = nil
local Gun = game.ServerStorage.Revolver
local Kills = 8 --The amount of kills someone needs to win


function RoundTimer()
	Round = RoundTime
	for i,v in pairs(game.Players:GetPlayers()) do --Get the players
		local randomSpawn = math.random(1, 6) --6 Spawn locations
		if randomSpawn == 1 then
			SpawnSelected = Spawn1
		end
		if randomSpawn == 2 then
			SpawnSelected = Spawn1
		end
		if randomSpawn == 3 then
			SpawnSelected = Spawn1
		end
		if randomSpawn == 4 then
			SpawnSelected = Spawn1
		end
		if randomSpawn == 5 then
			SpawnSelected = Spawn1
		end
		if randomSpawn == 6 then
			SpawnSelected = Spawn1
		end
		game.Players.PlayerAdded:Connect(function(plr)
			plr.CharacterAdded:Conect(function(chr)
				local CharHumanoid = chr:WaitForChild("HumanoidRootPart")
				CharHumanoid.CFrame = SpawnSelected.CFrame
				GunClone = Gun:Clone()
				GunClone.Parent = chr
			end)
		end)
	end
	while Round > 1 do
		wait(1)
		Round = Round - 1
		print(Round)
	end
	if Round < 1 then
		print("Round over")
		for i,v in pairs(game.Players:GetPlayers()) do
			game.Players.PlayerAdded:Connect(function(plr)
				plr.CharacterAdded:Connect(function(chr)
					local CharHumanoidRootPart = chr:WaitForChild("HumanoidRootPart")
					CharHumanoidRootPart:Destroy()
				end)
			end)
		end
	end
end

game.Players.PlayerAdded:Connect(function()
	local playerCount = #game.Players:GetPlayers()
	print(playerCount)
	if playerCount == 1 and Round < 1 then
		Intermission = IntermisssionTime
		while Intermission > 1 do
			wait(1)
			Intermission = Intermission - 1
			print(Intermission)
		end
		if Intermission == 1 then
			print("Intermission over")
			RoundTimer()
		end
	end
end)
2 Likes

First off, why are you using a for i,v in pairs loop and then have a function inside of it. I’ve never seen that before. If anything, you want to use for i,v in pairs after the character loads in. You then need to say if v.Name == “HumanoidRootPart” then
v:Destroy()

1 Like
for i,v in pairs(character:GetChildren()) do
    if v.Name == "HumanoidRootPart" then
       v:Destroy()
    end
end
1 Like

You don’t need to use a for loop. you could just do FindFirstChild.

for i, v in pairs (player.Character:GetChildren()) do
         if v:IsA("HumanoidRootPart") then
                --do whatever
         end
end```
If you want to use a for loop then do it like that^
2 Likes