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

How to use variable to get to something?

Asked by
DanzLua 2879 Moderation Voter Community Moderator
8 years ago

Hey, this might be an easy question to be answered.

Lets say that

player = "DanzLua"

How would I use the variable "player" (which is causing an error because it's a string value, i'm guessing) to get to get to game.Players.DanzLua

I tried

game.Players(player)

but that doesn't seem like the correct way to do this. Thanks.

Edit: Let me try to explain it a bit more. I have a StringValue with the value of "DanzLua" In a script there is a variable which is player=script.Player (Player is the name of the StringValue) I am trying to get to games.Players.DanzLua by using game.Players(player.Value)

Error: 17:13:50.503 - Workspace.FunnyMoon.Script:2: attempt to call field 'Players' (a userdata value) 17:13:50.503 - Stack Begin 17:13:50.504 - Script 'Workspace.FunnyMoon.Script', Line 2 17:13:50.504 - Stack End

Line causing error:

local player = game.Players(script.Parent.Player.Value)
0
you should use [ ] instead of ( ) for the line causing error if you want to link something that requires the name from a reference. starlebVerse 685 — 8y

2 answers

Log in to vote
1
Answered by 8 years ago
Lol, cool answer!

Okay, this is sorta simple, and I'll walk you through the steps.

First of all, how can we use a string to find a player in Players? Well, there is more than one way.

  • First way,
game.Players:FindFirstChild("Player1")--Looks for first player named this. If the player does not exist it will return nil.
This uses a string.
  • Second way,
game.Players["Player1"]
I don't personally use this, but this should be almost the same as the FindFirstChild, but it will error if Player1 does not exist.
  • Third way,
game.Players:WaitForChild("Player1")--Waits for Player1 to exist, and stops the script until this happens.
Use this often

To answer your question,

To use a variable, all you have to do is throw your variable anywhere where you can throw a string.

Your script will look like this with the fix,

player = "DanzLua"
local player = game.Players:FindFirstChild(player)-- Will be nil if player doesn't exist.
It is however important to note that you can't get anything that's not a direct child of Players from players using any of these methods.

Normally though, you would want to check for players names with the PlayerAdded event.

You didn't really clarify what you were trying to do, so if you need more help let me know.

Side note,

Only Local Scripts can access LocalPlayer, so to get new players, use the PlayerAdded event unless you're using a Local Script. If you need help with something like this, ask another question. Questions are what SH is for!

Good Luck!

0
Thank you sir, this has been a very annoying problem that i've been having for a long time and every time i've encountered it I had to find a way to go around it. You have made my dream of having a watching moon posible :) https://gyazo.com/8280c8e389ee9f22627024a4b130d6b0 DanzLua 2879 — 8y
0
<3 User#11440 120 — 8y
Ad
Log in to vote
1
Answered by 8 years ago

Easy, just make a variable and link it to the target player!

local player = game.Players.DanzLua -- Links to a Player named "DanzLua"

Or, if you want to link it to yourself (LocalPlayer)

local player = game.Players.LocalPlayer -- Links to the player this script is running in (That's yourself!)

Also, using variables is useful when you are trying to make longer links. It would your shortcut for the link so you won't have to write it multiple times! I hope this helps! If you have any questions, please leave it in the comments! Thank you.

0
Thanks for answering, but that quite isn't what i'm looking for. Let me try to explain it a bit more. I have a StringValue with the value of "DanzLua" In a script there is a variable which is player=script.Player (Player is the name of the StringValue) I am trying to get to games.Players.DanzLua by using game.Players(player.Value) (edited the post) DanzLua 2879 — 8y
0
I am getting an error when trying to run this, in edited post. DanzLua 2879 — 8y

Answer this question