Finding a specific player with a string

local CoinflipBox = script.Parent
local Player2Name = script.Parent.Player.Text -- the player that owns the cf
local CoinflipAnimation = game:GetService("ReplicatedStorage").RemoteEvents.CoinflipAnimation
local Players = game:GetService("Players")

local Player2
CoinflipBox.MouseButton1Click:Connect(function(player)
	Player2 = Players:FindFirstChild(Player2Name) -- problem
	CoinflipAnimation:FireServer(player , Player2)
end)

So i made the text in a textbox a players name and i wanna get that player whenever a button is pressed how would i do that

2 Likes

You could use a function like the following:

function getPlayerFromName(nameOfPlayer)
	local playerList = game.Players:GetPlayers()
	
	for _, player in pairs(playerList) do
		if player.Name == nameOfPlayer then
			return player
		end
	end
	--If no matching player is found, this function returns nil by default
end

Just pass in a string and it will search for a Player object with a corresponding Name

2 Likes

I don’t think your issue is the “Player2Name” variable.

In your FireServer parameters, you’re sending the “Player2Name” variable second. You’re also passing a nil parameter (player).

Can you share your OnServerEvent code please?

1 Like

This is not necessary. FindFirstChild should be used instead.

2 Likes

It’s not guaranteed that all children of Players are Player objects though. Its better practice to get all the players using :GetAllPlayers(). That is the specific reason the method exits.

1 Like
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CoinflipCreate = ReplicatedStorage.RemoteEvents.CoinflipCreate
local CoinflipCancel = ReplicatedStorage.RemoteEvents.CoinflipCancel
local CoinflipAnimation = ReplicatedStorage.RemoteEvents.CoinflipAnimation
local CoinflipBox = ReplicatedStorage.Gui.CoinflipBox
local PlayerText = CoinflipBox.Player
local Players = game:GetService("Players")

local CoinflipBoxClone
CoinflipCreate.OnServerEvent:Connect(function(player, Amount, CoinflipColor)
	local Coinflips = player.PlayerGui.Coinflip.Frame.Coinflips
	local Money = player.NonPvPStats.Money
	if Money.Value >= tonumber(Amount) then
		Money.Value = Money.Value - Amount
		for i , GetPlayers in pairs (Players:GetPlayers()) do 
			PlayerText.Text = player.Name
			CoinflipBox.Text = "$"..Amount
			CoinflipBox.BackgroundColor3 = (CoinflipColor) or Color3.fromRGB(255, 255, 255)

			CoinflipBoxClone = CoinflipBox:Clone()
			CoinflipBoxClone.Parent = GetPlayers.PlayerGui.Coinflip.Frame.Coinflips
		end
		else
		print("to poor")
	end
end)

local Player1Money
local Player2Money
CoinflipAnimation.OnServerEvent:Connect(function(Player1, Player2)
	Player1Money = Player1.NonPvPStats.Money
	Player2Money = Player2.NonPvPStats.Money
	if Player1Money and Player2Money then
		print("Player1 Money",Player1Money.Value,"Player2 Money", Player2Money.Value)
	end
end)

CoinflipCancel.OnServerEvent:Connect(function(player, Amount)
	local Coinflips = player.PlayerGui.Coinflip.Frame.Coinflips
	local Money = player.NonPvPStats.Money
	if CoinflipBoxClone then
		Money.Value = Money.Value + Amount
		CoinflipBoxClone:Destroy()
	end
end)
1 Like

so do i do wat he told me to or stick with wat i had originally

1 Like

When you use :FireServer, you don’t need the first argument. When it is passed to the server, the first argument is always the player that fired the server. On your client script, it should be :FireServer(Player2).

Also ensure you’re defining your Player2 variable.

You should also iterate through all players to find if the player exists. You should also use tolower or toupper so if the user doesn’t enter the same upper/lowercase letters, it will still find the user. Basically the same as what @East98 said, just adding the tolower.

local function getPlayerFromUsername(string)
    for i,player in pairs(game.Players:GetPlayers()) do
        if tolower(player.Name) == tolower(inputString) then
            return player
        end
    end
end

local player = getPlayerFromUsername(script.Parent.Text) -- assuming script.Parent is a textbox or is modifiable by the client.

if player then
    print('Player was found!', player.Name)
else
    print('No player was found!', script.Parent.Text)
end
1 Like

how would i define players1 value then

It’s already passed to the server when a client calls the server

Client:

remoteEvent:FireServer('Cool!')

Server:

remoteEvent.OnServerEvent:Connect(function(playerWhoFired, string)
    print(playerWhoFired.Name, string) --> 7z99, Cool!
end)
1 Like

o ok thanks for the useful information ill use it in later scripts but yk how to do the original thing

1 Like

Oh I think I get it. MouseButton1Click doesn’t pass the user who clicked the button as an argument.

1 Like

thats works fine im talking bout


Player2 = Players:FindFirstChild(Player2Name)
2 Likes

Are you ever inputting a string? It’s going to return nil if it doesn’t find the player with the EXACT same name, including cases and spaces.

1 Like

i dont see the problem because i dont think u can make the same name with diffrent cased letters and im also in testing mode so its just me

If you’re inputting “rOastdbacOn”, it won’t work as the cases are different. It has to be “Roastdbacon” exactly.

If it still doesn’t work, try pringing Player2Name

is pringing where u convert all the letters to lowercase?

What do you mean?

Just add “print(Player2Name)” inside of the MouseButton1Click event and then check the output.

o ok i thought it was a new term that the coding department uses

it works it prints the name of the person who created the box ( which was me)
now wat