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

How can I find a player in the workspace with a variable? [Answered]

Hi! This is a very basic question, but I'm very new to scripting. I have already assigned a variable to the person playing's name property (LocalPlayer.name), but I now want to use that to get to the player's model in the workspace. This is what I have:

local playername = game.Players.LocalPlayer.name

function teleport()
	game.Workspace.playername.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(0,50,0))
end

Now, I know the issue is that I can't simply put "workspace.playername," because it looks for an object called "playername" in the workspace. If I put in my own username, the script works perfectly fine. Is there any other way that I can accomplish this using the name of the person playing?

1 answer

✓ Accepted answer

To index anything with a non-string, use brackets. e.g. workspace[playername]

However the solution is to use the property LocalPlayer.Character, which gets the workspace model you want. Do not try to use names because if a different object has their name you will get wrong results.

5 comments

0

Wow, Thanks! Both of your answers work in-game!

0

Accept answer?

0

Sorry, I've been trying to figure out how to do that for 10 minutes XD

0

Do you know how to do that?

0

Nevermind, I figured it out. I was using firefox, and it wasn't showing me the button. I switched to Google Chrome and it came up.

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