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

How to find a player in workspace by finding the localplayer?

local player = game.Players.LocalPlayer.Name --Find Player name
local nameofplayer = game.Workspace:FindFirstChild(player) --Get the player in workspace 

if script.Toggled.Value == 1 then
	
print(nameofplayer)

end

Error (Line 2): Argument 1 missing or nil

2 comments

0

You can get a Player's Character by the 'Character' property of a Player ( Player.Character ). :)

0

Can I have an example? Or a revision to my original script?

5 answers

✓ Accepted answer

Maybe this will work

local player = game.Players.LocalPlayer --Find Player name
local nameofplayer = player.Character.Name--Get the player in workspace 

if script.Toggled.Value == 1 then

print(nameofplayer)

end

1 comment

0

All localPlayers have names, and the workspace player model names are the same.

Is this in a localscript? It seems like it isn't if it's finding player as a nil value. If it is in a localscript, it may be firing too fast before the character/player actually loads.

If you're simply looking for a name, a revision to ghost's script; you don't need the name of the character since it simply inherits it from the player object. Just pull the name from the player object you already defined.

local player = game.Players.LocalPlayer 

if script.Toggled.Value == 1 then
	print(player.Name)
end

I'm guessing you want to find the character of the player, in order to make it work on a server and not just in studio you must get player with this.

local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

"character.Name" is the the player's name.

Change it to this:

local player = game.Players.LocalPlayer.Name --Find Player name local nameofplayer = game.Workspace:FindFirstChild(player:FindFirstChild("Character").Name) --Get the player in workspace

if script.Toggled.Value == 1 then

print(nameofplayer)

end

1 comment

0

local nameofplayer = game.Workspace:FindFirstChild(player:FindFirstChild("Character").Name)

LocalPlayer names can be accessed with: local plrName = game.Player.LocalPlayer.Name And the character can be accessed with local character = workspace:WaitForChild(plrName) Hope that helps.

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