How do I reference a player using a string?

Hello!

So I’m making a system which allows staff at a school to award detentions, however I have come to the issue when referencing the player. I have a UI for the staff member to type the name of the student; and here’s the following code for when the user has chosen to click “send”:

script.Parent.MouseButton1Click:connect(function(plr)
local v = game.Players.LocalPlayer
for i,v in pairs(game.Players:GetPlayers()) do
local name = script.Parent.Parent.TextBox.Text
name.leaderstats.Detentions.Value = name.leaderstats.Detentions.Value + 1
end
end)

I get the following error:

Players.E_Link.PlayerGui.StaffMenu.LogAnIncident.TextButton.LocalScript:5: attempt to index field ‘leaderstats’ (a nil value)

And I’m pretty sure this is because the code cannot find the player using the string provided.

Thanks,
E_Link

3 Likes

You’re trying to index a string’s “leaderstats” field, yet a string doesn’t have such a field.

You first need to find the player object who that name belongs to, let’s write a separate function for that:

local function getPlayerFromName(name)
   -- loop over all players:
   for _, player in pairs(game:GetService("Players"):GetPlayers()) do
      -- if their name matches (case insensitive), return with that player:
      if player.Name:lower() == name:lower() then
          return player
      end
   end
   -- if we reach the end of the for-loop, no player with that name was found
end

Then your code becomes:

script.Parent.MouseButton1Click:connect(
   function()
      -- find player from textbox text:
      local player = getPlayerFromName(script.Parent.Parent.TextBox.Text)
      -- if the player exists, increase their Detentions value in leaderstats:
      if player then
          player.leaderstats.Detentions.Value = player.leaderstats.Detentions.Value + 1
      end
   end
)
22 Likes

game.Players[name].leaderstats.Blah.Value = 0

2 Likes

I’m attempting to access the “Detentions” intvalue which is a child of leaderstats

Thank you, I tainted your code a little and now it works!

1 Like

BuildThomas’s code is more robust, but mine works fine assuming the names are all correct. But yeah,

Game.Players.ColdSmoke
Game.Players["ColdSmoke"]

Both do the same thing, only the second one can use any string which means you can make it dynamic based on a variable. You also might want to include beforehand

if Game.Players:FindFirstChild(name) then
 --stuff
end

Just to make sure the player didn’t leave the game.

3 Likes

Among the currently supplied solutions, mind that:

  • game:GetService("Players")[playerName] will priorize properties over instances - if a player named “Name” enters your game, game:GetService("Players")["Name"] will give you the name of the Players service, and not that player instance. It also errors if there’s no property with the supplied name, nor any child instance with the supplied name. If your staff members type in the username incorrectly, the code errors.

  • game:GetService("Players"):FindFirstChild(playerName) only checks if a child instance can be found with that name - it’s less prone to errors than the previous listed solution, but if you for some odd reason have non-Player instances directly inside of the Players service, this can give you those instances by mistake if a staff member types its name in. Weird case, but worth mentioning. :slight_smile:

6 Likes

If your problem was solved, press the check mark (aka. “Solution” button) next to the reply that solved your problem instead of renaming the topic’s name to “[SOLVED]”.
chrome_2018-01-23_14-59-48