Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How to reference a player using a text box?

Asked by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

**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.****

0
Can you put your text inside of a code block please? It is really hard to read what you have here. laughablehaha 494 — 4y
0
my example has the code in a code block only changed value = player to value = player.Name royaltoe 5144 — 4y

1 answer

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago

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
Thank you, it works. raid6n 2196 — 4y
Ad

Answer this question