Referencing Player Names

Say I were to give myself some money inside a game, I would create a function so that I would be able to use it in the console. How would I be able to define a name that starts with a number? Like my name for example, a script like

function givecash()
game.Players.3Dmodedmaster.leaderstats.cash.Value += 100
end

wouldn’t work because of the simple fact that my name starts with a number. How would I be able to fix this so that it would work? I have already checked many sources and I couldn’t find any answers to this.

function givecash()
    local cash = game:GetService("Players"):FindFirstChild("3Dmodedmaster").leaderstats.cash
    cash.Value = cash.Value + 100
end
1 Like

You can do what Tomarty did (all though it might be a bad habit to use :FindFirstChild() when it’s not needed) or you can simply do

game.Players["3Dmodedmaster"].leaderstats.cash

This will work, although I still recommend FindFirstChild, especially for arbitrary player names:

3 Likes

Just to make the function a bit more versatile, you can pass the player as an argument (meaning having a parameter to get this argument in the givecash function), so you can give cash no matter the player.

function givecash(player)
   if not player then
     return
   end
   player.cash.Value + 100
end

givecash(game:GetService("Players"):FindFirstChild("3Dmodedmaster"))

Very good point actually, even though it’s unnexplainably rare for someone with the name ‘FindFirstChild’ for example to show up, your way is more secure.

Even so, using FindFirstChild is much more safer. For example. if the player left the game before the script references the player (without FindFirstChild ofc), it will potentially break the entire script. So as a safety precaution one should always use FindFirstChild to reference something for the first time, even if they know its almost certainly there.

It’s not safer here, since he is immediately indexxing the return from FindFirstChild. If the player is not in-game, his code is equivalent to nil.leaderstats.

Here’s a helpful thread that talks about this issue, as well as how to use FindFirstChild safely:

3 Likes