Jump to content

math.random issue [Solved]


Recommended Posts

Posted (edited)

Hi. I'm back! Really getting hooked on scripting Lua ;)

Well, my new problem is a math.random, problem. When I try to get the value of my randMoney, I ALWAYS get a nil value.

I am not sure what I am doing wrong, because of my other math.random works just fine.

Anyways.. Here is the script. Hope someone can help me out on this one :lol:

function drugRunner (thePlayer, command) 
    local randDrugMarker = { 
    {-720,961,12}, 
    {-705,968,12}, 
    {-698,954,12}, 
    {-711,942,12} 
} 
  
    local randDrugMoneyGet = math.random(50,500) 
  
local randomX, randomY, randomZ = unpack(randDrugMarker[math.random(#randDrugMarker)]) 
local theDrugMarker = createMarker ( randomX, randomY, randomZ, "cylinder", 1.5, 255, 255, 0, 170 ) 
local randMoney = unpack(randDrugMoneyGet[math.random(#randDrugMoneyGet)]) 
end 
  
function markerHit ( thePlayer ) 
    if(randMoney) then 
            outputChatBox ("The Number Is:" .. tostring(randMoney)) 
    else 
            outputChatBox ("Value is nil!") 
    end 
end 
  
addCommandHandler("drugrun", drugRunner) 
addCommandHandler("a", markerHit) 

Edited by Guest
Posted

Your code is a bit weird.... try this:

local randDrugMarker = { 
    {-720,961,12}, 
    {-705,968,12}, 
    {-698,954,12}, 
    {-711,942,12} 
} 
  
function drugRunner (thePlayer, command) 
local randomX, randomY, randomZ = unpack(randDrugMarker[math.random(#randDrugMarker)]) 
local theDrugMarker = createMarker ( randomX, randomY, randomZ, "cylinder", 1.5, 255, 255, 0, 170 ) 
end 
addCommandHandler("drugrun", drugRunner) 
  
function markerHit ( thePlayer ) 
local randMoney = math.random(50,500) 
    if(randMoney) then 
    outputChatBox ("The Number Is:" .. tostring(randMoney)) 
    else 
    outputChatBox ("Value is nil!") 
    end 
end 
addCommandHandler("a", markerHit) 

Posted
local randDrugMoneyGet = math.random(50,500) 
  
local randMoney = unpack(randDrugMoneyGet[math.random(#randDrugMoneyGet)]) 
  

I don't understand what you want to do with the randMoney variable , seems like you are trying to get a random from randDrugMoneyGet which is impossible because its an integer not a table.

randDrugMoneyGet is an integer ranging between 50 and 500.

maybe explain what you want with randMoney.

you should do the following:

local randMoney = math.random(50,500) 

EDIT: iam too late :D

EDIT2:

Btw...

How come that randDrugMarker is outside the function, when randMoney is inside the function?

randDrugMarker have nothing to do with randMoney.

randDrugMarker is the coordinates of the marker randMoney is the random money.

And dont double post.

Posted

I didnt double post did I? :S

Yeah, I know what randDrugMarker and randMoney does :P I was just wondering why randDrugMarker can be outside the function where it is used, when randMoney is inside the function where it is used.

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