Jump to content

Get state from table


FuriouZ

Recommended Posts

Posted

Hello!

I have problem, i don't know how to set up table..

I need to get engine state randomly from table like 1 = true, 2 = false, but I don't understand how to do it

                local engineStates = { 
                                     [ 1 ] = true, 
                                     [ 2 ] = false, 
                                     }   
                local state = unpack ( engineStates [ math.random ( #engineStates ) ] )                                   
                    setVehicleEngineState ( source, state ) 

Thanks!

Posted

You can't unpack a boolean, the unpack function only unpacks tables (if you think in it, it makes complete sense). It should be like this:

setVehicleEngineState ( source, engineStates[ math.random ( #engineStates ) ] ) 

I hope your code isn't designed the way you showed, you shouldn't be defining the table whenever that code is executed, rather define it once in a place that can be accessed by that piece of code. The current way works, but the fact that it works doesn't mean you should do it that way.

I used to know how to code, but then I took an arrow in the knee.

Project Redivivus - Remaking Old School MTA With New Code

MTA 0.6 Nightly 1 released

Posted
local engineStates = { 
    [ 1 ] = true, 
    [ 2 ] = false, 
 }  
setVehicleEngineState ( source, engineStates[ math.random ( #engineStates ) ] ) 

well? :?

 DUyJ810.gif

Posted

I think you need to try this

  
Table = {} 
Table[1]={true} 
Table[2]={false} 
  
for i, v in pairs(Table) do 
  
  setVehicleEngineState ( theVehicle, math.random(v) ) 
end 
  

If it wont work tell me as i didnt tested it

Script Trading Status

Successful Trading : 26

Scam : 0

On Sale : Banking System SQL Based

On Sale : Housing System MySQL Based

Download and Support my new script on Community : http://community.mtasa.com/index.php?p=resources&s=details&id=11686

SQL Based Housing

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