Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to find part of a string and assign it to a variable?

Is it possible to find just part of a string? Like if the string is "58", are you able to assign "5" to a variable and "8" to another variable?

1 comment

0

I'm not an expert at this stuff, but I guess no because 58 is a string by itself.

1 answer

✓ Accepted answer

You would need to learn string manipulation here. https://developer.roblox.com/api-reference/lua-docs/string

If its only a two digit number than you could do something like:

local first_var
local second_var
local string_to_parse = "58"
-- Get the first character, assign it to first_var
first_var = tonumber(string_to_parse:sub(1,1))
-- Get the second character, assign it to second_var
second_var = tonumber(string_to_parse:sub(2))
-- Convert the variables from strings to numbers
-- Add the numbers together
print(first_var + second_var)

Output: 13

My sample was just an example so do not think of it as a smart way of approaching what you want. Study the different methods of string manipulations in the link I provided.

2 comments

1

Thank you, that works!

0

no problem bud, have fun

This discussion is preserved as it was. New answers, comments, and votes are closed.