How to get part of a name of an instance

i was wondering if i could get a part of a name of an instance in roblox, there is a string value for the players name but the string value has a number in the name. i need to get that number and and send it back so if it is their turn, it switches to the next one. i do not know what to do, i have been thinking of just putting int values in them but i want to see if i could do this first! (first post, sorry if its bad)

1 Like

You can use tostring() to convert a number to a string and tonumber() to convert a string to a number. If you need the number in the name, use string.sub() if the entire name is NOT something that can be translated to a number from a string with tonumber().

Or if you don’t know the exact position of a number and the only numbers in the string are ones you need you could do
StrPart = string:match(“+%d”)
string patterns are handy: Strings | Documentation - Roblox Creator Hub

its not working, the sub is getting the the 7th letter in the name (Player1) and the PlayersTurn Value is 1 but it prints no (object is Player1)
code:
if string.sub(object.Name, 7, 7) == object.Parent.PlayersTurn.Value then
print(“yes”)
else
print(string.sub(object.Name, 7, 7)) --prints 1
print(object.Parent.PlayersTurn.Value) --prints 1
print(“no”) --still returns to no
end

You have to either turn the string.sub(object.Name, 7, 7) into a number using tonumber or turn object.Parent.PlayersTurn.Value into a string using tostring. This is because numbers and strings will always return false when compared to each other, even if they contain the same value.

So new code:

if string.sub(object.Name, 7, 7) == tostring(object.Parent.PlayersTurn.Value) then

	--continue with the rest of your code

PlayersTurn.Value is in intvalue though

Exactly, but string.sub(object.Name, 7, 7) will return a string value, and since strings are not and will never be the same as integers, which is what IntValue.Value returns, that conditional check there will always print no.

it works! thank you @goldenstein64 and everybody else who participated

1 Like