Deagle Posted May 10, 2011 Share Posted May 10, 2011 Hello, I am using the chatbox_ads script from 50p, but I saw that its happening that an ad (the same one) to repeat in the chatbox even for 3 times. I want a different ad everytime, at that period time I set in the main.lua My question is, can anyone script it so that the ads to not repeat or 50p can you update it with this option ? https://community.multitheftauto.com/index.php?p= ... ils&id=186 Link to comment
Deagle Posted May 11, 2011 Author Share Posted May 11, 2011 BUMP! Cmon man, I dont know scripting, but I think that you need just to modify some lines here : function advert( ) local rnd = math.random( 1, #adMessages ) outputChatBox( adMessages[ rnd ], getRootElement(), 255, 255, 255, true ) end Link to comment
karlis Posted May 11, 2011 Share Posted May 11, 2011 (edited) with this version rarely one ad can repeat 2times once a cycle of all add table. probably there would be more efficient way of 2nd table, but cba doing that, as this should work just fine. local tbl={ } local tblPos=1 function refreshList() local count=1 for _,val in pairs(adMessages) do --pairs automatically randomizes the list tbl[count]=val count=count+1 end end function advert( ) outputChatBox( adMessages[tblPos], getRootElement(), 255, 255, 255, true ) tblPos=tblPos+1 if tblPos>#adMessages then tblPos=1 refreshList() end end addEventHandler("onResoruceStart",getRootElement(),refreshList) if you don't want that 1 add cant appear more then once a cycle tru all list use this version WARNING:NEVER USE IT WITH ONLY 1 ADD OR INF LOOP WILL BE MADE. local lastRnd=0 function advert( ) local rnd = math.random( 1, #adMessages ) while rnd == lastRnd do rnd = math.random( 1, #adMessages ) end outputChatBox( adMessages[ rnd ], getRootElement(), 255, 255, 255, true ) lastRnd=rnd end Edited May 11, 2011 by Guest Link to comment
BinSlayer1 Posted May 11, 2011 Share Posted May 11, 2011 well, math.random is obviously used to get a random number and it is possible to get the same number a few times in a row.. I suppose you could take them 1 by 1 instead of being all randomized. Try the following code, even though I didn't test it and I'm not sure if this is what you want to do.. function advert( ) nr = nr + 1 if nr == #adMessages then nr = 0 end outputChatBox( adMessages[nr], getRootElement(), 255, 255, 255, true ) end Right before this function, add the following line: nr = 0 Link to comment
Deagle Posted May 12, 2011 Author Share Posted May 12, 2011 Thank you very much karlis , the first script worked, it is doing what I wanted! Thanks again Link to comment
Aibo Posted May 12, 2011 Share Posted May 12, 2011 WARNING:NEVER USE IT WITH ONLY 1 ADD OR INF LOOP WILL BE MADE. why not prevent it then, instead of writing warnings in caps? local lastRnd=0 function advert() local rnd = math.random( 1, #adMessages ) while rnd == lastRnd and #adMessages > 1 do rnd = math.random( 1, #adMessages ) end outputChatBox( adMessages[ rnd ], getRootElement(), 255, 255, 255, true ) lastRnd=rnd end Link to comment
karlis Posted May 12, 2011 Share Posted May 12, 2011 WARNING:NEVER USE IT WITH ONLY 1 ADD OR INF LOOP WILL BE MADE. why not prevent it then, instead of writing warnings in caps? local lastRnd=0 function advert() local rnd = math.random( 1, #adMessages ) while rnd == lastRnd and #adMessages > 1 do rnd = math.random( 1, #adMessages ) end outputChatBox( adMessages[ rnd ], getRootElement(), 255, 255, 255, true ) lastRnd=rnd end dont think that should be necessary for the use it serves, just would consume more resoruces. EDIT: slight improvement local lastRnd=0 function advert( ) local rnd repeat rnd = math.random( 1, #adMessages ) until rnd ~= lastRnd outputChatBox( adMessages[ rnd ], getRootElement(), 255, 255, 255, true ) lastRnd=rnd end 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