Determining Player Name from String

Hey, I’ve been trying to figure out how I’d determine the player name (essentially the whole name excluding the “Ball” part)

Part.Name = Player.Name.."Ball"

From another script, I want to grab the player’s name from the part name only. I know there’s a workaround using objectvalues or stringvalues, but I’m wondering how I’d go about getting the player name from a string like this.

1 Like

You’ll want to grab a so-called substring of the entire string, namely you want to cut off the “Ball” part from the string, the last four characters.

To pick a part of a string and not the full string, you should use the string.sub method. This will go like this:

local playerName = string.sub(Part.Name, 1, #Part.Name - 4)

Meaning that you take the substring of Part.Name, from the first to the total number of characters in the string minus 4, so we cut off the last four characters.

3 Likes

Ah, okay, thanks.
I’ve always wondered how people did that.

Wait…

1 actually refers to the first letter in the string for once rather than 0?

Lua starts its indexing at 1 instead of 0.

2 Likes

Learn something new everyday. What a bizarre thing after learning C# and Java.

lua is generally pretty weird

playerName = part.Name:sub(1, -5)
4 Likes

local plrName = string.gsub(part.Name,"Ball","")

This will return a string with anywhere it finds “Ball” at the end removed. The only downside is if “Ball” is also located somewere in the player’s name on a rare occasion.

-- use $ to anchor ball to the end

part.Name:gsub('Ball$', '')