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

Can't get the Character of a Player?

Asked by
Sindrotex -41
3 years ago
Edited 3 years ago

I have a tool in starterpack and inside there a server script. My idea was to get the player from the backpack and then the character, but it errors saying nil or that it's not a valid member of DataModel. I found the player, but I can't use Player.CharacterAdded:Wait() or Player.Character for some reason.

local tool = script.Parent
local player = script:FindFirstAncestorWhichIsA("Player")
local character = player.Character or player.CharacterAdded:Wait()
1
The problem is because `player` is nil. I need more details from you to give you a solution. Optikk 499 — 3y
0
bro I literally said I found the player and it's "FindFirstAncestorWhichIsA("Player")" Sindrotex -41 — 3y
0
Try `print(type(character))` and see what is printed NotTheChara 191 — 3y
0
Sindro, you grabbed the player wrong. WideSteal321 773 — 3y
0
ight how do I grab it right Sindrotex -41 — 3y

2 answers

Log in to vote
0
Answered by
Sindrotex -41
3 years ago

both player and character print right, but if I try to parent a model FROM the tool to the character it breaks saying "attempt to index nil with character"

(tool variable is the model)

local tool = script.Parent
local player = script:FindFirstAncestorWhichIsA("Player")
local character = player.Character or player.CharacterAdded:Wait()
print(player)
print(character)
tool.Parent = character

prints but immediately after it says nil so...

Ad
Log in to vote
0
Answered by 3 years ago
local tool = script.Parent
local player = script:FindFirstAncestorWhichIsA("Player")
local character = game.Workspace:FindFirstChild(player.Name)

Answer this question