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

How do I get a players name and fix this error?

Asked by 4 years ago

I thought this would be easy and made it very quickly but found that it didnt work. So , I tested it alot of times to see if there is a mistake but in the end I just couldnt find it even after I tried.

So if anyone knows whats wrong with my script please explain to me. Help would be appreciated! Thanks.

This script is to get the players name.

local plr = game:GetService("Players").LocalPlayer
local name = plr.Name

print(name)

it just gives me this error and idk how to fix 17:25:22.297 - Workspace.Script:2: attempt to index nil with 'Name' 17:25:22.297 - Stack Begin 17:25:22.298 - Script 'Workspace.Script', Line 2 17:25:22.298 - Stack End

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I'm going out on a limp here, and I'm going to say that you didn't define where the player's name was:

local player = game.Players:GetService("Players").LocalPlayer
local name = player.Name

print(name)

Or I would just do this:

LocName = local name
print(LocName)

Also the trouble may be because "name" is already a variable.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

LocalPlayer ONLY works on LOCAL SCRIPT. Notice that local script cannot run on workspace, u will need to put it in: StarterGui ,StarterPack, or StarterPlayerScript

local plr = game:GetService("Players").LocalPlayer
-- or you can do game.Players.LocalPlayer
local name = plr.Name

print(name)
Log in to vote
0
Answered by
zomspi 541 Moderation Voter
4 years ago

Use PlayerAdded

0
Yes, and that is a comment, use answer when u explain how to do it Nguyenlegiahung 1091 — 4y
0
I tried playeradded and waitforchild but it doesnt work RebornedSnoop 175 — 4y

Answer this question