Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
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!

0
.Character is not a property of tables. INOOBE_YT 387 — 5y
0
ok then loop through the players and affect them one by one User#21908 42 — 5y
0
I edited my answer to better help you. If it answers your question please accept. Otherwise comment how I can help you better. User#21908 42 — 5y

3 answers

Log in to vote
1
Answered by 5 years ago

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
0
The first good answer Vulkarin 581 — 5y
0
Thanks, just doing my duty! KardashevScale 110 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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!

0
Im trying to affect all the players zMadZeus 6 — 5y
Log in to vote
-2
Answered by 5 years ago
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.

0
You literally copied his code. Why? User#21908 42 — 5y
0
I did copy it but I added a :GetChildren() function. User#21998 0 — 5y
0
If he is trying to return the players from a table using :GetPlayers(), why would you suggest :GetChildren()? KardashevScale 110 — 5y
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. User#21998 0 — 5y
View all comments (3 more)
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 Vulkarin 581 — 5y
0
also getplayers is the recommended method for getting the children of the players service User#21908 42 — 5y
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 User#19524 175 — 5y

Answer this question