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

Help with a Function?

I want it to take all the players not just the player model ( sorry for my english , bassicaly , when I enter the game and use it on someone it aint work and I guess it's becuase of ~~~~~~~~~~~~~~~~~ (game.Workspace.Player)


the function is this one 

p = game.Players:getPlayerFromCharacter(game.Workspace.Player)


How to make it take all player models not just the oe in the solo play?

2 answers

To get all the players you can use :GetChildren() and pairs to get all the children. This is an example of GetChildren.

local workspacechildren = workspace:GetChildren()

print(#workspacechildren) --Should print how much things are direct children of workspace.

This is pairs:

for _,v in pairs(workspace:GetChildren()) do
	print(v.Name) --Should print all of the Items in Workspace.
end

Now we get all the players:

for _,v in pairs(workspace:GetChildren()) do
	if v.ClassName == "Player" then --Just incase.
		print(v.Name)--Print all of the players names.
		v.Character.Torso.CFrame = CFrame.new(0,0,0) --Teleport the Characters to 0,0,0
		Instance.new("ForceField", v.Character)--Give the players forcefields.
	end
end

Just edit the above script.

I hope this helps!

EDIT

Perci1 Found an error. I meant to say game.Players:GetChildren().

for _,v in pairs(game.Players:GetChildren()) do --I just copy and pasted my last script so I didn't have to type the whole thing again. I should be more careful next time! BTW I do know there are no players in workspace. I also know that ":IsA("Player")" works also. It's just I'm used to "ClassName" and I used "v" instead of "player" like how adark did because it's just shorter.
	if v.ClassName == "Player" then --Just incase.
		print(v.Name)--Print all of the players names.
		v.Character.Torso.CFrame = CFrame.new(0,0,0) --Teleport the Characters to 0,0,0
		Instance.new("ForceField", v.Character)--Give the players forcefields.
	end
end

2 comments

1

There aren't Players in workspace -- only character models. These have the classname of "Model", since they're models. Just loop through Players directly.

0

Additionally, you should use the 'IsA' method when checking what type an Object is, never the ClassName itself.

Lord's answer is incorrect. Like Perci commented, there are no Player objects in the Workspace.

To get a Table of all the Players in a game, call GetPlayers on Game.Players. This functions identically to GetChildren, except that it will only get Player objects:

for _, player in ipairs(Game.Players:GetPlayers()) do
	print(player.Name)
end

1 comment

0

Well, now both of ours is correct. I corrected my answer. I copied and pasted my 2nd example script and I forgot to replace "workspace" with "game.Players".

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