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
5 years ago
Edited 5 years ago

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

This is what it looks like:

01local function Name(name)
02  for _, player in pairs(game:GetService("Players"):GetPlayers()) do
03     if player.Name:lower() == name:lower() then
04         return player
05     end
06  end
07end
08 
09 
10script.Parent.MouseButton1Click:connect(
11    function()
12     local player = Name(script.Parent.Parent.UsernameHandler.Text)
13     if player then
14        game.Workspace.Value.Value = player
15        script.Parent.Parent.Username.Text = game.Workspace.Value.Value
16     end
17  end
18)

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 — 5y
0
my example has the code in a code block only changed value = player to value = player.Name royaltoe 5144 — 5y

1 answer

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 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:

1player = game.Players:FindFirstChild(nameOfPlayerGoesHere)

Instead of having the function.

Here's the code anyway:

01players = game:GetService("Players"):GetPlayers()
02 
03--this function gets the player object when inputed a player name
04local function Name(name)
05        for _, player in pairs(players) do
06                if player.Name:lower() == name:lower() then
07                       return player
08                end
09        end
10end
11 
12 
13script.Parent.MouseButton1Click:connect( function()
14        local player = Name(script.Parent.Parent.UsernameHandler.Text)
15 
View all 26 lines...
1
Thank you, it works. raid6n 2196 — 5y
Ad

Answer this question