For i,v loop not working properly

Hello! I am having trouble with my custom admin commands. I am trying to loop through all of the players and respawn them, but it only respawns me.
image
Even when I print the names, it still doesn’t respawn “KLB_Tester.”

My Code:

if char2 == "all" then
		for i,v in pairs(game.Players:GetPlayers()) do
			local Character = v.Character
			local Humanoid = Character.Humanoid

			plr:LoadCharacter()
			print(v.Name)
		end
		print("DONE")
		return
	end

The full code:

_G.Respawn = function(char2, plr)
	if plr == nil then
		if game.Players:FindFirstChild(char2.Name) then
			local Character = char2.Character
			local Humanoid = Character.Humanoid

			char2:LoadCharacter()
		end
		
		return
	end
	
	if char2 == "all" then
		for i,v in pairs(game.Players:GetPlayers()) do
			local Character = v.Character
			local Humanoid = Character.Humanoid

			plr:LoadCharacter()
			print(v.Name)
		end
		print("DONE")
		return
	end
	if char2 == "others" then
		for i,v in pairs(game.Players:GetChildren()) do
			if v.Name ~= plr.Name then
				local Character = v.Character
				local Humanoid = Character.Humanoid

				plr:LoadCharacter()
			end
		end

		return
	end
	if char2 == "me" then
		if game.Players:FindFirstChild(plr.Name) then
			local Character = plr.Character
			local Humanoid = Character.Humanoid

			plr:LoadCharacter()
		end

		return
	end
	
	for i,v in pairs(game.Players:GetChildren()) do
		local vName = string.lower(v.Name)
		if vName:sub(1, #char2) == char2 then -- 1 is where is starts, #char2 is how far to serch. 
			local Name = v.Name
			local Character = v.Character
			local Humanoid = Character.Humanoid
			
			plr:LoadCharacter()
			return
		end
	end
	
end

I am really stuck on this. Please help! :frowning:

for _, v in pairs is for dictionary, use ipair for arrays.

for i, v in ipairs(game.Players:GetPlayers()) do
   print(v)
end

You have done plr:LoadCharacter(). Instead you should do v:LoadCharacter()

1 Like

Testing that right now, thank you!

You have loaded your character twice instead of loading each players character

1 Like

The “plr” is an object, there-for I don’t need to do v or plr.Name. :slight_smile:

Oh, how silly of me. Thank you!

Edit: I made too many commands to focus on my small mistakes.

1 Like

@OP, you should also either wrap the code in a pcall or use if statements, because it can lead to an error!

for i,v in pairs(game:GetService('Players'):GetPlayers()) do
       pcall(function()
		local Character = v.Character
		local Humanoid = Character.Humanoid

		v:LoadCharacter()
		print(v.Name)
		end
	end)

	return
end
1 Like

ipair is for arrays and pair is for dictionnary, it is called ipair because arrays have an index, dictionaries do not.

2 Likes