Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to reference a player using a text box?

**Hello! I am currently having trouble with my script.

This is what it looks like:

local function Name(name)
  for _, player in pairs(game:GetService("Players"):GetPlayers()) do
     if player.Name:lower() == name:lower() then
         return player
     end
  end
end


script.Parent.MouseButton1Click:connect(
	function()
     local player = Name(script.Parent.Parent.UsernameHandler.Text)
     if player then
        game.Workspace.Value.Value = player
        script.Parent.Parent.Username.Text = game.Workspace.Value.Value
     end
  end
)

When I try to submit the username, the output says 13:08:25.136 - Players.raid6n.PlayerGui.TableFinder.Background.TextButton.LocalScript:18: bad argument #3 to 'Value' (string expected, got Object)"

Thank you for reading, have a great day.****

2 comments

0

Can you put your text inside of a code block please? It is really hard to read what you have here.

0

my example has the code in a code block only changed value = player to value = player.Name

1 answer

✓ Accepted answer

Here's the script, but I'm not sure why you are getting the player object. Either way, you can get the player object by doing this:

player = game.Players:FindFirstChild(nameOfPlayerGoesHere) 

Instead of having the function.

Here's the code anyway:

players = game:GetService("Players"):GetPlayers()

--this function gets the player object when inputed a player name
local function Name(name)
        for _, player in pairs(players) do
                if player.Name:lower() == name:lower() then 
                       return player 
                end 
        end 
end


script.Parent.MouseButton1Click:connect( function() 
        local player = Name(script.Parent.Parent.UsernameHandler.Text) 

        if player then --we found the player object 
              --change the stringValue (?)'s value to be the players name
              --this is where it was erroring because we were trying to put the player object into the stringValue instead of the player name string. 
              --making the value player.Name will fix the error
              game.Workspace.Value.Value = player.Name

              --make text the players name.
              script.Parent.Parent.Username.Text = 
              game.Workspace.Value.Value
        end 
end )

1 comment

1

Thank you, it works.

This discussion is preserved as it was. New answers, comments, and votes are closed.