How do I reference a player using a string?

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
)
21 Likes