Help with string.split()

So, i’m making a lyrics game as a test, and i’m having the issue that words that are in new lines count as the same “word”.
Example:

I’m using (string.split(“string whatever”, " "))
but I can’t seem to be able to get these parts.

image

What do you mean?

I heard thats for making a new line, im trying to let the players put lyrics and be able to loop through every single word.

My problem is specifically detecting these.

If they put the lyrics theirselves you could just add a disclaimer to add spaces.

1 Like

Can you add the string you are attempting to split to your original post?

It’s user inputted.

But here’s an example:

Lyrics lol

Don’t cry, snowman, not in front of me
Who’ll catch your tears if you can’t catch me, darling
If you can’t catch me, darling

Don’t cry, snowman, don’t leave me this way
A puddle of water can’t hold me close, baby
Can’t hold me close, baby

I want you to know that I’m never leaving
'Cause I’m Mrs. Snow, 'til death we’ll be freezing
Yeah, you are my home, my home for all seasons
So come on, let’s go

Let’s go below zero and hide from the sun
I love you forever where we’ll have some fun
Yes, let’s hit the North Pole and live happily
Please don’t cry no tears now, it’s Christmas, baby

My snowman and me
My snowman and me
Baby

Don’t cry, snowman, don’t you fear the sun
Who’ll carry me without legs to run, honey
Without legs to run, honey

Don’t cry, snowman, don’t you shed a tear
Who’ll hear my secrets if you don’t have ears, baby
If you don’t have ears, baby

I want you to know that I’m never leaving
'Cause I’m Mrs. Snow, 'til death we’ll be freezing
Yeah, you are my home, my home for all seasons
So come on, let’s go

Let’s go below zero and hide from the sun
I love you forever where we’ll have some fun
Yes, let’s hit the North Pole and live happily
Please don’t cry no tears now, it’s Christmas, baby

My snowman and me
My snowman and me
Baby

1 Like

You can actually see there’s a character there, but I think I can’t use multiple arguments in string.split()
image

Split the inputted string using “\n”, then join all of the split strings together with a space.

2 Likes

how would I do that, I don’t quite understand?

Nvm, doesn’t work. I tried “/n” and copying the little character at the end.

Use \n, not /n. \n is the line break character.

2 Likes

You can use this as a replacement for string.split.

local output = {}
string.gsub(yourString, "%S+", function(s) table.insert(output, s) end)

Try this code out, seems to work.

local Lyrics = [[Don’t cry, snowman, not in front of me
Who’ll catch your tears if you can’t catch me, darling
If you can’t catch me, darling

Don’t cry, snowman, don’t leave me this way
A puddle of water can’t hold me close, baby
Can’t hold me close, baby

I want you to know that I’m never leaving
'Cause I’m Mrs. Snow, 'til death we’ll be freezing
Yeah, you are my home, my home for all seasons
So come on, let’s go

Let’s go below zero and hide from the sun
I love you forever where we’ll have some fun
Yes, let’s hit the North Pole and live happily
Please don’t cry no tears now, it’s Christmas, baby

My snowman and me
My snowman and me
Baby

Don’t cry, snowman, don’t you fear the sun
Who’ll carry me without legs to run, honey
Without legs to run, honey

Don’t cry, snowman, don’t you shed a tear
Who’ll hear my secrets if you don’t have ears, baby
If you don’t have ears, baby

I want you to know that I’m never leaving
'Cause I’m Mrs. Snow, 'til death we’ll be freezing
Yeah, you are my home, my home for all seasons
So come on, let’s go

Let’s go below zero and hide from the sun
I love you forever where we’ll have some fun
Yes, let’s hit the North Pole and live happily
Please don’t cry no tears now, it’s Christmas, baby

My snowman and me
My snowman and me
Baby]]

wait(3)

local Players = game:GetService'Players'
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild'PlayerGui'
local ScreenGui = Instance.new("ScreenGui", PlayerGui)
local TextLabel = Instance.new("TextLabel", ScreenGui)
TextLabel.AnchorPoint = Vector2.new(0.5, 0.5)
TextLabel.Size = UDim2.new(0.5, 0, 0.8, 0)
TextLabel.Position = UDim2.new(0.5, 0, 0.5, 0)
TextLabel.Text = ""

local Lines = string.split(Lyrics, "\n")
for i, Line in pairs(Lines) do
	local Words = string.split(Line, " ")
	for i, Word in pairs(Words) do
		TextLabel.Text ..= Word .. (i == #Words and "" or " ")
		wait()
	end
	TextLabel.Text ..= (i == #Lines and "" or "\n")
end
1 Like

My system is the same, but thanks. :smiley:
also, you should always do iPairs in this case.

I don’t see how using ipairs instead of pairs is any different here

for the order of the text and stuff, it’s just better

I don’t think there’s a difference, both pairs and ipairs go through the table in order, and as far as I know the only difference is that ipairs will stop iterating if there is a nil value in the middle of the table

Doesn’t apply to Roblox but in Lua 5.2+, there’s a __pairs and __ipairs metamethod and in Lua 5.3+ ipairs respects __index and pairs (AFAIK) don’t.
In Roblox Luau, ipairs have been optimised (apparently) to work for “arrays”. pairs are intended for “dictionaries”.

The order of pairs are undefined. pairs being in numerical order is actually an implemention detail.

The order in which the indices are enumerated is not specified, even for numeric indices . (To traverse a table in numeric order, use a numerical for or the ipairs function.)

The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields.

3 Likes

Anyways that’s what I’m making lol

Doing this is honestly so addicting lol