Splitting String Values

Hi there! I have looked over this article Strings | Documentation - Roblox Creator Hub and I would like to know if this is possible?
I have a value saved to all players called “PlayerAnim” and everyone has a value for it, like DefaultAnim, or RthroAnim, and when people buy stuff it will load the character and say
robloxapp-20210124-1140223.wmv (974.2 KB)
I already added the “ation” by adding,

script.Parent.TextLabel.Text = "You Purchased the " .. player.PlayerAnim.Value .. "ation!!!"

Now what I want to know is, is there a way to count the number of letters (4 would be the number) in the value from back to front(or right to left) and then put a space in between?

Also, in the video I posted, does it come up as something you need to download or something you can just click on and it plays?

splitTable = string.split(player.PlayerAnim.Value, “A”)
spaced = splitTable[1] … " A" … splitTable[3]

–If player.PlayerAnim.Value contains “playerAnim” then spaced will contain “player Anim”

Edit: the video comes up as a file to download on my phone
Edit2: However, your value has to contain only 1 capital A for the script to work

So if I have something like the AstronautAnim, it will not work?

Also, I tried doing it and I am getting this error

splitTable = string.split(player.PlayerAnim.Value, 'A')
	spaced = splitTable[1] .. " A" .. splitTable[3]
	script.Parent.TextLabel.Text = "You Purchased the " .. spaced .. "ation!!!"

I saw that you put three dots instead of the two concatenate dots if that makes a difference, it wasn’t working for me, (the three dots)

  12:06:56.420  Players.MRKYLO20.PlayerGui.LoadingScreenGUI.LoadingScreenFrame.LocalScript:8: attempt to concatenate string with nil  -  Client - LocalScript:8
  12:06:56.420  Stack Begin  -  Studio
  12:06:56.420  Script 'Players.MRKYLO20.PlayerGui.LoadingScreenGUI.LoadingScreenFrame.LocalScript', Line 8  -  Studio - LocalScript:8
  12:06:56.420  Stack End  -  Studio

It is throwing the error on the middle line

No, but it is possible to write a script that can work with that

Weird, string.split should have returned a table with 3 elements. Try printing splitTable[1], splitTable[2] and splitTable[3] before spaced = …

It would probably need to be a letter counter type thing

it printed

  12:15:43.695  Mage  -  Client - LocalScript:8
  12:15:43.695  nim  -  Client - LocalScript:9
  12:15:43.695  nil  -  Client - LocalScript:10

from my knowledge, A should be number 2 and nim should be 3 right?

Change splitTable[3] to splitTable[2].

1 Like

Excellent! It worked! Do you have any idea what I should do the the AstronautAnim?

If the roblox documentation of string.split would have been right, then splitTable[1] would be “Mage”, splitTable[2] would be “” and splitTable[3] would be “nim”. It should have returned 3 elements, but it only returned 2.

you could make “A” in AstronautAnim, lowercase, if it doesn’t matter for the rest of your code. You could then try making the first letter of the string, uppercase, before printing it, though I am unsure of how to do it.
Edit: But that would also mean that only the first letter can be a capital “A”. To make the code to work with capital "A"s, you need to make

Edit2: @Validark found a better soultion.

You should check out the string-patterns-reference article to learn how to use Lua’s pattern matching. Here’s an example of how you could solve this problem using string.match:

local function getAnimPurchaseMsg(name)
    local animName = string.match(name, "^(.+)Anim$") or
        error("Anim was not found at the end of: `" .. name .. "`")

    return "You Purchased the " .. animName .. " Animation!!!"
end

print(getAnimPurchaseMsg "PlayerAnim")
print(getAnimPurchaseMsg "DefaultAnim")
print(getAnimPurchaseMsg "RthroAnim")
print(getAnimPurchaseMsg "AstronautAnim")
print(getAnimPurchaseMsg "AnimAnim")
print(getAnimPurchaseMsg "AnimAnimation") -- this should error

Try this code in an online interpreter

2 Likes