Jump to content

math.random with addEventHandler


adamb

Recommended Posts

So basically,

i have these

function CreateX()

function CreateZ()

function CreateY()

So, i want to make the script use math.random to select a random one of those three above to start with addEventHandler onResourceStart tho i have no idea how to do it.. Can anyone help me out?

Link to comment
  • Moderators

NOT tested.....

  
local myFunctions = {} -- store the functions 
  
myFunctions[1]= function (player)--CreateX 
    if player then 
        outputChatBox(getPlayerName(player) .. ", executed: CreateX") 
    else 
        outputChatBox("CreateX") 
    end 
end 
  
myFunctions[2]= function (player)--CreateY 
    if player then 
        outputChatBox(getPlayerName(player) .. ", executed: CreateY") 
    else 
        outputChatBox("CreateY") 
    end 
end 
  
myFunctions[3]= function (player)--CreateZ 
    if player then 
        outputChatBox(getPlayerName(player) .. ", executed: CreateZ") 
    else 
        outputChatBox("CreateZ") 
    end 
end 
  
addEventHandler("onResourceStart",resourceRoot, 
function () 
    local number = math.random(1,#myFunctions) -- a random number from 1 t/m the amount of functions in the table. 
    myFunctions[number]() 
end) 
  
addEventHandler("onPlayerDamage",root, -- random event....... 
function () 
    myFunctions[math.random(1,#myFunctions) ](source) -- compressed.  
end) 
  

Link to comment

What you call a function is just a variable that holds a value of type function (this doesn't apply only to functions). Always make sure you declare variables as local, unless you need them elsewhere out of that scope.

Link to comment

So, out of that, i did this, i get a syntax error near 'myFunctions'

myFunctions[1]= startcrazy 
local startResource ( crazy ) 
  
myFunctions[2]= startmugging 
local startResource ( mugging ) 
  
myFunctions[3]= startgang  
local startResource ( gang ) 

Link to comment

Not Tasted.

Resources = { 
    [1] = "crazy"; 
    [2] = "mugging"; 
    [3] = "gang"; 
}; 
  
addCommandHandler ( "random", 
    function ( player ) 
        local random = Resources [ math.random ( #Resources ) ] 
        outputChatBox ( ( random or "Error" ), player, 255, 255, 0, true ) 
    end 
) 

Link to comment

Done this, i get Bad arguement on line 10 @ startResource tho...

Resources = { 
    [1] = "crazy"; 
    [2] = "mugging"; 
    [3] = "gang"; 
}; 
  
function callouts() 
    local res = getResourceFromName("crazy", "mugging", "gang") 
    local random = Resources [ math.random ( #Resources ) ] 
    startResource ( random ) 
    outputChatBox ( "Dispatch here, all units standby for callout" ) 
end    
addEventHandler ( "onResourceStart", resourceRoot, callouts ) 
setTimer (addEventHandler, 300000, 0) 
  
function this ( theResource ) 
    local they = getResourceFromName ( "calloutsbeta" ) 
if getResourceFromName then 
    restartResource(they) 
    end 
end 
setTimer (this, 420000,0  ) 

Link to comment

Resources = { 
    [1] = "crazy"; 
    [2] = "mugging"; 
    [3] = "gang"; 
}; 
  
addEventHandler ("onResourceStart",resourceRoot, 
    function ( ) 
        setTimer ( 
            function (  ) 
            local random = Resources [ math.random ( #Resources ) ] 
            local res = getResourceFromName( random ) 
             if not res then return end 
            startResource ( res ) 
            outputChatBox ( "Dispatch here, all units standby for callout" ) 
        end,300000,0 
        ) 
    end 
) 

Does that what are you trying to do ?

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...