Jump to content

Random Messages


knightscript

Recommended Posts

Hello, im trying to make a random messages script, im using the following code:

local mensajes = 
    { 
           {"test"}, 
           {"test2"} 
} 
local mensajes = mensajes[math.random( #mensajes)] 
local elmensaje = unpack(mensajes) 
function map () 
outputChatBox(elmensaje,getRootElement(), 0, 0, 255, true) 
end 
  
setTimer ( map, 6000, 0 ) 

But it is only showing 1 message, how can i fix this?

Thanks

Link to comment

Add more text`s the script should work.

local mensajes = { 
    {"test1"}, 
    {"test2"}, 
    {"test3"}, 
    {"test4"}, 
    {"test5"}, 
    {"test6"}, 
    {"test7"} 
}; 
  
setTimer(outputChatBox, 6000, 0, unpack(mensajes[math.random(#mensajes)]), getRootElement(), 0, 0, 255, true); 

Link to comment

I just tested the code in lua IDE(ofcourse with math.randomseed()).

And it seems math.random() indeed generate random number but sometimes, strangely, same message is outputted.

However, the below code works fine(atleast in IDE).

  
local mensajes = 
    { 
           {"test"}, 
           {"test2"} 
} 
local mensajes = mensajes[math.random( #mensajes)][1] 
function map () 
    outputChatBox(mensajes,getRootElement(), 0, 0, 255, true) 
end 
  
setTimer ( map, 6000, 0 ) 
  

And btw, there is no need to put messages in another table, it will work without it too.

local mensajes = 
    { 
           "test", 
           "test2" 
} 
local mensajes = mensajes[math.random( #mensajes)] 
function map () 
    outputChatBox(mensajes,getRootElement(), 0, 0, 255, true) 
end 
  
setTimer ( map, 6000, 0 ) 

Link to comment

This should work 100%.

local messages = { 
    {"1"}, 
    {"2"}, 
    {"3"}, 
    {"4"}, 
    {"5"}, 
    {"6"}, 
    {"7"} 
} 
  
function test () 
outputChatBox(unpack(messages[math.random(#messages)]), getRootElement(), 255, 255, 255, true) 
end 
setTimer(test, 6000, 0) 

Link to comment

@1LoL1

That will indeed work. :)

@ knightscript

The reason why your code didn't work is simple, you are actually never overwrighting the first generated random number.

local mensajes = 
    { 
           {"test"}, 
           {"test2"} 
} 
local mensajes = mensajes[math.random( #mensajes)] -- lets say the random is 1 
local elmensaje = unpack(mensajes) 
function map () 
outputChatBox(elmensaje,getRootElement(), 0, 0, 255, true) -- here you will get message number 1 
end 
  
setTimer ( map, 6000, 0 ) -- Here you are repeating the function but not the random number as it is outside the function. So our random number will forever be equal to 1 and with it, our message will always be the same. 
  

By putting the math.random() function inside the function 'map', you will generate a new random number each time the function execute/run.

local mensajes = 
    { 
           {"test"}, 
           {"test2"} 
} 
function map () 
local mensajes = mensajes[math.random( #mensajes)] 
local elmensaje = unpack(mensajes) 
outputChatBox(elmensaje,getRootElement(), 0, 0, 255, true) 
end 
  
setTimer ( map, 6000, 0 ) 

Why I didn't notice it before....

Link to comment

@knightscript, anytime. :)

You are cent percent right in saying lua is the easier language compare to others, if not easiest.

Keep learning!

@Tekken

I did a simple test and got the below,somewhat strange, results.

setTimer(outputChatBox, 6000, 0, tostring(unpack(mensajes[math.random(1, #mensajes)])), getRootElement(), 0, 0, 255, true); -- This [b]doesn't[/b] generate random numbers/messages 

Where as

setTimer(function() outputChatBox(tostring(unpack(mensajes[math.random(1, #mensajes)])), getRootElement(), 0, 0, 255, true) end, 6000, 0); -- This [b]do[/b] generate random numbers/messages 

Looks like math.random() doesn't generate any new random when directly brought to setTimer.

For example, as per the theory.

setTimer(math.random, 1000, 0, 1, 100)  -- will [b]not[/b] generate random numbers 

and

setTimer(function() math.random(1,100) end, 1000, 0)  -- [b]will[/b] generate random numbers 

strange...

Link to comment
Thanks guys! you are really helping me out!.

mta_screen_2016_01_25_06_04_55.png

LUA is so easy in comparation to other programming languages, I´m scripting this gamemode without any base and I hope i´m able to open it soon, I have learned a lot with the of many of you and I just want to thank you all! :)

EDIT: Credits for the HUD: #RooTs

Thanks :mrgreen::mrgreen:

Link to comment
Reported and the issue is this: "You always get the same number because math.random is called only once, and THAT value generated ONCE is then passed to setTimer."

Ah, I have already guessed that but certainty will help further in scripting. Haha, we learned something today. :)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...