Generating a random number for damage

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
AlexNerd
Posts: 39
Joined: Sat Jun 29, 2019 11:46 am

Generating a random number for damage

Post by AlexNerd »

Apologies if this has been answered before or if it should be common programming sense, but I'm wondering how to generate a random number in a dialogue lua script. I'm trying to make it so attacks do damage in a range (say, between 1 and 6 damage) and I'm wondering what command to put in for that. Thanks!
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Generating a random number for damage

Post by Tony Li »

Hi Alex,

Use the "math.random" function. Examples:

Code: Select all

Variable["Damage"] = math.random(1, 6)
or:

Code: Select all

TakeDamage(math.random(1, 6))
AlexNerd
Posts: 39
Joined: Sat Jun 29, 2019 11:46 am

Re: Generating a random number for damage

Post by AlexNerd »

Thank you! I wasn't sure if that function worked in lua
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Generating a random number for damage

Post by Tony Li »

Sorry, I had typed mathf a couple times in my previous reply (now fixed). In Lua, it's math.random, not mathf.random.
AlexNerd
Posts: 39
Joined: Sat Jun 29, 2019 11:46 am

Re: Generating a random number for damage

Post by AlexNerd »

So I'm trying to do

Code: Select all

Variable["BaseDamage"] = mathf.random(1, 4);
but it's not changing the default variable value (0). I put it in the "script" area, is that not where I'm supposed to put it?
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Generating a random number for damage

Post by Tony Li »

Remove the "f" from mathf. Sorry, I had typed it wrong in my first reply.
Post Reply