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

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

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

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.

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)

Use PlayerAdded

2 comments

0

Yes, and that is a comment, use answer when u explain how to do it

0

I tried playeradded and waitforchild but it doesnt work

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