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

gsub Random number each match?

newstring = string.gsub(oldstring, "hello", number())

number() is a function that returns a random number I'm trying to make it so i replace all matches of "hello" to a random number, however whenever I use the script above it just turns every "hello" into one randomly generated number.

e.g Current string:

"hello hi hello hi hello hi"

what I'm trying to get:

"4 hi 7 hi 2 hi"

what happens:

"3 hi 3 hi 3 hi"

1 answer

✓ Accepted answer

Don't call the function

Rather than using number() which evaluates to a random number, use number which is a function that evaluates to a random number when called. Functions are first-class types in Lua, and can be passed about just like any other variable.

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