Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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?

0
I'm not an expert at this stuff, but I guess no because 58 is a string by itself. Aqu_ia 39 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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.

1
Thank you, that works! bumblebeejake 5 — 5y
0
no problem bud, have fun YTRaulByte 389 — 5y
Ad

Answer this question