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

How do you get players in workspace?

I have the script to where it puts a boolian in the player but I need to check if the players value is true or not then teleport the player if false. Idk the proper way of doing this. leik?

The only thing that's erroring is :GetChildren()

error: Value is not a valid member of Camera

Spawns = game.Workspace.Spawns
Players = game.Players:GetChildren()
AFKCheck = game.Workspace:GetChildren()

for PlayerTeleport = 1, #Players do
	if game.Workspace:FindFirstChild(Players[PlayerTeleport].Name) then 
		if AFKCheck[PlayerTeleport].Value == false then 
			Players[PlayerTeleport].Character.Torso.CFrame = Spawns[_G.StartingPlaces[math.random(1,#_G.StartingPlaces)]].CFrame * CFrame.new(0,8,0)
			else
		end
	end
end

2 answers

game.Players.PlayerAdded:connect(function(plr)
	
	wait(1)
	player = game.Workspace:FindFirstChild(plr.Name)

end)

Try that out, it will get the players from the Player table and it will find it in Workspace, hope this helps! This isn't the whole question, just the fix by the way.

To get each player in Workspace, do this:

local Players = game.Players:GetPlayers()

for i,Player in ipairs (Players) do
	local Character = Player.Character
end

to count players, use: game.Players.NumPlayers()

8 comments

0

I've never used ipairs, how would this fit into the script?

0

SwaggyDuckie if it isn't a Local Script Player.Character won't work unless you use GetPlayerFromCharacter()

0

You don't need the parenthesis after NumPlayers since NumPlayers is a property.

1

@User, it'd work, you didn't read my script correctly probably.

0

I wouldn't really recommend using 'ipair'; When the current Position in a Table hits nil, while using 'ipairs', the loop will stop. :P

0

"pairs" might do same.

0

'pairs' doesn't do the same as 'ipairs'; From my testing with both recently, 'ipairs' (I knew for a long time) stops when the Table position hits nil, then stops the loop, however, 'pairs' appears to skip the 'nil' position of the table, and continues the loop to the next position within the Table, until it reaches the end of the Table.

0

Oh, Okay.

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