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)
First of all, how can we use a string to find a player in Players? Well, there is more than one way.
game.Players:FindFirstChild("Player1")--Looks for first player named this. If the player does not exist it will return nil.
game.Players["Player1"]
game.Players:WaitForChild("Player1")--Waits for Player1 to exist, and stops the script until this happens.
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.
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.
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!
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.