knightscript Posted January 24, 2016 Share Posted January 24, 2016 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
Tekken Posted January 24, 2016 Share Posted January 24, 2016 Try this: local mensajes = { {"test"}, {"test2"} }; setTimer(outputChatBox, 6000, 0, tostring(unpack(mensajes[math.random(1, #mensajes)])), getRootElement(), 0, 0, 255, true); Link to comment
Army@1 Posted January 24, 2016 Share Posted January 24, 2016 (edited) Who says recurrence of same number/message is not random? Edited January 24, 2016 by Guest Link to comment
knightscript Posted January 24, 2016 Author Share Posted January 24, 2016 Its displaying the same message Link to comment
Tekken Posted January 24, 2016 Share Posted January 24, 2016 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
Army@1 Posted January 24, 2016 Share Posted January 24, 2016 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
knightscript Posted January 24, 2016 Author Share Posted January 24, 2016 Hello, I dont know why but it is still showing the same message again and again: Link to comment
Tekken Posted January 24, 2016 Share Posted January 24, 2016 Can you show me the exact script you were using when you take this photo? Argument 2 is getRootElement, and shouldn't return '0'. Link to comment
1LoL1 Posted January 24, 2016 Share Posted January 24, 2016 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
Army@1 Posted January 24, 2016 Share Posted January 24, 2016 @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
Tekken Posted January 24, 2016 Share Posted January 24, 2016 ...Why I didn't notice it before.... You are right, but why didn't this work ? setTimer(outputChatBox, 6000, 0, unpack(mensajes[math.random(#mensajes)]), getRootElement(), 0, 0, 255, true); 1 Link to comment
knightscript Posted January 25, 2016 Author Share Posted January 25, 2016 Thanks guys! you are really helping me out!. 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 Link to comment
Army@1 Posted January 25, 2016 Share Posted January 25, 2016 @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
Tekken Posted January 25, 2016 Share Posted January 25, 2016 Might be a mta team mistake and/or error from setTimer, I am going to report this issue. Link to comment
Army@1 Posted January 25, 2016 Share Posted January 25, 2016 Might be a mta team mistake and/or error from setTimer, I am going to report this issue. Possibly, go for it. Link to comment
#RooTs Posted January 25, 2016 Share Posted January 25, 2016 Thanks guys! you are really helping me out!. 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 Link to comment
Tekken Posted January 25, 2016 Share Posted January 25, 2016 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." Link to comment
Army@1 Posted January 25, 2016 Share Posted January 25, 2016 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now