Can I get the fourth character of a name?

I have this keypad setup, and I have a simple for loop that checks if any of the clickDetecs have been clicked, but I was wandering how to get the number of it. Not the parent, but the number that the parents name has.
KeyPad

1 Like

Could I see your script please?

You could use string.sub to only get the number

1 Like
string.split(Instance.Name, "")[4]

or

string.sub(Instance.Name, 4, 4)

Make sure to define or rename Instance.

1 Like

As reply above said, you should use string.sub

for index, child in ipairs(KeyPad:GetChildren()) do
   if child:IsA("BasePart") then
      local numberStr = child.Name:sub(4,#child.Name)
      local number = tonumber(numberStr)
   end
end
2 Likes

I would probably use sub with 4 and the length of the Key.Name in case more than 9 keys are added.

1 Like
child.Name:sub(4,#child.Name)

Just so you’re aware string.sub()'s second parameter defaults to the subject string’s length.

local String = "Hello world!"
print(string.sub(String, 4) == string.sub(String, 4, #String)) --true

It’s also marginally more efficient to call the string library’s operations as functions instead of as methods.

3 Likes