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

attempt to index local 'PlrChar' (a nil value) ?

local Plrs = game.Players:GetPlayers()
local PlrChar = Plrs.Character
local PlrTorso = PlrChar:WaitForChild("Torso")

I alredy defined "Character" but it keeps saying that is nil!

I've tried it in two ways, changing from normal script to local script [and changing his parent, to ServerScriptService, or ReplicatedFirst] but the error is the same!

3 comments

0

.Character is not a property of tables.

0

ok then loop through the players and affect them one by one

0

I edited my answer to better help you. If it answers your question please accept. Otherwise comment how I can help you better.

3 answers

✓ Accepted answer

I'm pretty sure this was what you were trying to do:

game:GetService("Players").PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait() 
	local torso = character:WaitForChild('Torso') -- incase the torso is nil
end)

Put this script inside a folder, preferably in the "ServerScriptService". Your code is returning character as nil, because character is not a property or child of :GetPlayers(). Which returns an array of all the players in the server. A second method of doing this, would be using "in pairs" which is shown on the :GetPlayers() wiki itself:

https://wiki.roblox.com/index.php?title=API:Class/Players/GetPlayers

for _,v in pairs(game:GetService("Players"):GetPlayers()) do
	if v.Character then
		local character = v.Character
		local torso = character:WaitForChild('Torso')
	end
end

2 comments

0

The first good answer

0

Thanks, just doing my duty!

Your problem is that you are trying the get the Character from a list of players. You must first either loop through the players and get their characters that way or you should choose the particular player you want to affect. Hope this helps and have a great day scripting! Edit: if you want to affect all the players just loop through them like so:

local playersService = game:GetService("Players")-- get service is the recommended way of getting the players service
local players = playersService:GetPlayers()
for i, player in pairs(players) do -- for each player do the following code
	local char = player.Character
	-- and so on you get the idea
end

Hope this helps!

1 comment

0

Im trying to affect all the players

local Plrs = game.Players:GetChildren()
local PlrChar = Plrs.Character
local PlrTorso = PlrChar:WaitForChild("Torso")

Not sure if this is going to work or not but i don't really know what you're trying to ask and accomplish here.

7 comments

0

You literally copied his code. Why?

0

I did copy it but I added a :GetChildren() function.

0

If he is trying to return the players from a table using :GetPlayers(), why would you suggest :GetChildren()?

0

They're literally two different functions. Stop being nit picky about things. You either try something first to see if it works or not, if it doesn't then yes I'm wrong in my answer.

0

You changed to a function that does the same thing and didn't solve the problem in the first place that he's not iterating through the players

0

also getplayers is the recommended method for getting the children of the players service

0

Yea because if a Part is in there you'll get a Character is not a valid member of part. You also did copy the code, and giving OP code that is more likely to error

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