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

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

Asked by 6 years ago
Edited 6 years ago

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

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
6 years ago

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.

0
Wow, Thanks! Both of your answers work in-game! MrBlockyhead 84 — 6y
0
Accept answer? cabbler 1942 — 6y
0
Sorry, I've been trying to figure out how to do that for 10 minutes XD MrBlockyhead 84 — 6y
0
Do you know how to do that? MrBlockyhead 84 — 6y
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. MrBlockyhead 84 — 6y
Ad

Answer this question