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