game.Players:FindFirstChild() returning nil

Hey! I’m trying to get a player from game.Players using their names, but it keeps returning “nil”. Other thing to know is I’m trying to FireClient using their name through a server script.

I tried looking through the dev forum and Reddit and I can’t seem to find any solutions to my problem.

Here’s an example of my code:

local giveCashEvent = game:GetService(“ReplicatedStorage”):WaitForChild(“GiveCash”)

local playerName = script.Parent.Owner.Value

local getPlayer = game.Players:FindFirstChild(playerName)

local Credits = math.Random(10,15)

giveCashEvent:FireClient(getPlayer, Credits)
3 Likes

Not familiar with “Owner” Looks like something you set up.

If you have the character, use this to get player:

Players:GetPlayerFromCharacter (roblox.com)

If it is a localscript, use this:

Players.LocalPlayer (roblox.com)

What does playerName return then?
print(playerName)

You’re using curly-quotes in that first line.

local giveCashEvent = game:GetService(“ReplicatedStorage”):WaitForChild(“GiveCash”)
-- should be
local giveCashEvent = game:GetService("ReplicatedStorage"):WaitForChild("GiveCash")

i’ve tried your script with by changing getPlayer/ Credits

local giveCashEvent = game:GetService("ReplicatedStorage"):WaitForChild("GiveCash")

local playerName = script.Parent.Owner.Value

local getPlayer = game.Players:WaitForChild(playerName, 10)

local Credits = math.random(10,15)

giveCashEvent:FireClient(getPlayer,Credits)

and it works!
i used print() to test if it really works to the client when it was firedimage

1 Like

Yea, sorry I forgot to mention, “Owner” is a string variable. I don’t have the character, only the name and I am using a server script.

playerName returns the player’s name, but getPlayer returns nil.

I tried using your script, but it still returns the same thing as I tried with my code which is,

nil
FireClient: player argument must be a Player object

Also I’m still not an expert with coding at all, so what does the “10” do in getPlayer (playerName, 10)?

10 there is 10s timeout where if its been 10s and it doesn’t appear then the script will stop waiting for the child. studio take time to load all gui/ events and others so i put that there.

yea i use string value for it. not string variable cause if it is it will just be

local Owner = "Someone"

local playerName = Owner

did you copy/ paste my code there? if not maybe check your capitalization and if it still doesn’t work can you show me the full error so i would know which line it is

1 Like

Yea I copied and paste your code, still didn’t work. However I did already found a work around to my problem, so my script is working now, just a different way. Thank you for trying to help though, I really appreciate it!

1 Like

No problem! glad i could help, even though not much!

1 Like

This isn’t a Solution per say …
I believe what you was looking for was:

local getPlayer = game.Players:WaitForChild(playerName.Name)