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

gsub Random number each match?

Asked by
trecept 367 Moderation Voter
6 years ago
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

Log in to vote
2
Answered by 6 years ago

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.

Ad

Answer this question