String.split is great, but also highly recommend you read up on Lua patterns
e.g.
local str = 'Builderman:like patterns'
str:gsub('(.*):(.*)', function (user, reason)
print(
('The user \'%s\' would like you to know that they \'%s\''):format(user, reason)
) --[!] Prints: The user 'Builderman' would like you to know that they 'like patterns'
end)
Why should you care? You’ll be able to have more control over what you interpret from the string. For example, if you passed ‘Builderman:like patterns:) really important message here btw!’ to the above code or to string.split you would get:
--> In the case of string.split:
local str = 'Builderman:like patterns:) really important message here btw!'
local split = str:split(':')
local user = split[1] -- Would be: 'Builderman'
local reason = split[2] -- Would be: 'like patterns'
--> In the case of the gsub above
str:gsub('(.*):(.*)', function (user, reason)
--[!] user would be 'Builderman:like patterns'
--[!] reason would be ') really important message here btw!'
end)
As you can see, neither of these are perfect because we miss the ‘really important message’, but there’s no easy way to solve this issue in string.split unless you sanitise or only look for the first colon, concatenating the rest of the values. However, with pattern matching…
We know that Roblox usernames only contain alphanumeric characters, so we can alter our pattern matching accordingly:
local str = 'Builderman:like patterns:) really important message here btw!'
str:gsub('(%a*):(.*)', function (user, reason)
--[!] User is now defined as 'Builderman'
--[!] Reason is now defined as ') really important message here btw!'
print(
('The user \'%s\' would like you to know that they \'%s\''):format(user, reason)
) --[!] Prints: The user 'Builderman' would like you to know that they 'like patterns:) really important message here btw!'
end)
Voila, we have now avoided a potential disaster (dependent on what your code is being used for). Always look for edge cases!