I want to reference a player by a string value but have no idea and have been trying for ages. Does anyone know how to? (Local Script)
You can set the StringValue’s value to the player’s name. To get the player from the StringValue, just use Players:FindFirstChild(StringValue.Value)
Example:
local Players = game:GetService('Players')
local StringValue = -- Your string value
-- Setting the StringValue to the player
local MyPlayer = Players.LocalPlayer
StringValue.Value = MyPlayer.Name
-- Getting the player from a StringValue
local MyPlayer = Players:FindFirstChild(StringValue.Value)
-- Note: This method can return nil if the player no longer exists or the StringValue has not been set first.
This is getting a player from a string value, which is what I assume the OP is trying to ask.
Regarding OP: You can’t exactly “reference a player by a string value”, as a string value is just that: a string value, not a player reference.
The string value is a username.
If you look at the provided code sample more carefully, you’ll realise that the name of the LocalPlayer is being held in a StringValue and the player is later retrieved by performing a lookup under the Players service by the value of the StringValue. There is no attempted reference fetching here.
Would help to read the code first before commenting.
If you’re trying to reference a player, you can use ObjectValue. Take a look from this example:
local objV = script.ObjectValue
objV.Value = game.Players.LocalPlayer
Unfortunately, StringValue is different from ObjectValue. Similar to others have said, string value can only be valued with a string, not object.
It is inside a GUI. Using a Text Box.
Alright. You can refer to this code below:
--// Script must be parented to TextBox
local TextBox = script.Parent
TextBox.FocusLost:Connect(function()
if game.Players[TextBox.Text] then
print(TextBox.Text.." Player is found")
else
print(TextBox.Text.." Player is not found")
end
end)
Why don’t you try to use tostring
function?
local StringName = tostring(GUI.Frame.TextLabel.Text)
From my code, you can just do is:
local var
if game.Players[TextBox.Text] then
var = game.Players[TextBox.Text]
end
Can you elaborate on what I misinterpreted about the code in my comment? I stated it’s taking a string and getting a player from it, if that’s not another way to sum it up in English I’d definitely like to be enlightened. My second part was stating that a variable can’t have a string value and a reference to a player at the same time, which to my knowledge, is true. I interpreted OP’s question as that, but in OP’s response to my original comment I now understand his intention.
Thanks for the advice?
Which the provided code answered.
This part sounds as though the code sample is attempting to use the value of the StringValue in order to get a reference to the player, which it isn’t. It performs a lookup on Players by the value of the StringValue. I’m not sure where your point of disagreement comes in, if that was the intention of the remark, when the provided code sample addresses the issue.
Nowhere in the code is it suggested that two different values are trying to be put into one variable and a string isn’t being assigned to any variable either. MyPlayer remains a reference to a player; StringValue.Value remains a string. The remark is true, yes, but it’s mute because realhigbead isn’t doing that at all.
I wasn’t addressing his code when I stated that. I’m not sure how one can tie that part to the code provided, but from a reading perspective I can see how one might think I’m addressing the code when I commented that. I’ve edited it and I’ll clarify myself better next time.
Yes, the code answered what I assumed the OP was trying to ask. I didn’t state otherwise. Like I mentioned above, I misinterpreted OP’s question as a variable holding a player instance and a string value, or a string value referencing a player.
Thank you for elaborating, this was a lot more helpful in clarifying up the confusion than the previous remark of “read the code first before…”.
Why do you want to do this? Referencing by the actual player object would be best. If you have to keep any reference to the player outside the scope of the game, use the UserId.
Judging by these replies from OP and the accepted solution, I’d assume they’re using it for a GUI where you can type a player’s name into a TextBox to perform an action on them.