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

why I cant find players inside a workspace with for loop?

while wait() do
local players = game.Players:GetChildren()
for _,p in pairs(players) do
print(p)
if workspace:FindFirstChild(p) ~= nil then
print("1")
end
end
end

edit: my fix is to change the code to get the same results but I still don't understand why the first code doesn't work.

while wait() do

for _,player in pairs(game.Players:GetChildren()) do

if player.Character:FindFirstChild("Humanoid") ~= nil and player.Character ~= nil then

local torso = player.Character:FindFirstChild("UpperTorso")

local human = player.Character:FindFirstChild("Humanoid")

print(torso.Position)

end

end

end

3 comments

0

format pls

0

also if i deal with ur question in all technicality, players dont exist in the workspace, their characters do. if u want to find the players u go to game.Players; if u want to find their characters the easiest way would be to scan the workspace for an object matching the player's name, cus if u didnt know the character model's name is named after the player controlling it.

0

FindFirstChild takes a string of the name, you already have the player instance, but use GetPlayers instead. Then you just need to do player.Character.

2 answers

✓ Accepted answer

You can try this instead:

for i, model in pairs(workspace:GetChildren()) do
    for j, player in pairs(game:GetService("Players"):GetPlayers()) do
		if model.Name == player.Name then
			    -- Do something here
		end
	end
end

You would have to run a for loop through both the players and the children of the Workspace. If you're not used to this, it can be difficult.

1 comment

0

The Character that is added to Workspace has the same name as the Player who owns it.

The easiest way is to get the characters directly from the player instances you already have instead of iterating the workspace to try and match the names. Using FindFirstChild like you have, takes a string for the name, not an instance.

local PS = game:GetService("Players")
local players = PS:GetPlayers()
for _, player in pairs(players) do
	local char = player.Character
	if char then
		print("Character of", player.Name, "found")
	else
		print("Character of", player.Name, "not found")
	end
end

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