Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can't find all new players/characters in an in pairs loop?

In a local script I'm trying to make players transparent for certain players but when a new player joins or a character dies it can't find the character that died or player that joined. How do I make the script refresh to see new characters & players?

for i, v in pairs(game.Players:GetPlayers()) do
	print(v)
	local chr = plr.Character
	repeat wait() until chr.Humanoid
end

2 comments

0

What's your goal with this?

0

To make players transparent for certain players

1 answer

I am not sure what do you want to do but if i would want to find all the characters in game i would do this:

for index, player in pairs(game.Players:GetPlayers()) do -- for every single player in game
	local character = player.Character or nil -- makes variable, or nil is made for if player leaves during checking, it may give error: trying to acess nil with character
	if character ~= nil then -- if character exists then (~= means that variable on the left IS NOT variable on the right)
		local humanoid = character:FindFirstChild("Humanoid") -- creates variable for the humanoid
		if humanoid ~= nil then -- if humanoid is not nil and exists then
			print(character.Name) -- prints out the character
		end
	end
end

4 comments

0

You can use an ipairs loop, instead of a normal pairs loop.

0

Doesn't ipairs break the loop when hits nil? This may keep half of the players uncheckced, or am i wrong?

0

ipairs is recommended by Roblox themself. It's also just preference as pairs would have a microscopic difference. It is mainly used for numerals. But it is also efficient for arrays as well. Here's a screenshot: https://prnt.sc/t8sq04

0

Okay thanks didn't know but what happens when player leaves? It breaks the loop, isn't it a problem?

This discussion is preserved as it was. New answers, comments, and votes are closed.