Jump to content

Tables and setTimer


AfuSensi

Recommended Posts

Hi everyone,

I am trying to create a script where if you hit a marker, a random function will get triggered.

Now i stored my function names in a table, and added a function with the event onMarkerHit.

But every time i ride trough a marker, debugscript gives me this error: Bad argument @ 'setTimer' [Expected function at argument 1, got table].

My question is, how do i properly randomize the function name table, and start the function the randomizer chose?

My script(server):

markersize = 2 -- Set Marker Size -- 
respawntime = 10000 -- Set marker respawn time in milliseconds ( 1sec = 1000 ) -- 
  
  
  
  
  
  
functionstable =   { 
            [1]={"MassiveCar"}, 
            [2]={"Barrel"}, 
            [3]={"Haybale"}, 
            [4]={"Rocket"}, 
} 
  
  
  
  
  
  
allObjects = getElementsByType ( "object" ) 
  
  
function destroyothermarkers () -- Destroys all markers that are not from this script -- 
 previousmarkers = getElementsByType ("marker") 
 for _, prevmark in pairs( previousmarkers ) do 
 destroyElement ( prevmark ) 
end 
end 
-- setTimer ( destroyothermarkers, 1000, 1 ) 
addEventHandler ( "onResourceStart", getRootElement(), destroyothermarkers ) 
  
  
  
function checkforheads () -- Checks for heads, replaces it with markers and makes the heads invisible and with no collision -- 
  
    for _, t in pairs( allObjects ) do 
        if ( getElementModel (t) == 2908 ) then 
            x,y,z = getElementPosition (t) -- Gets xyz position from the heads 
            createdmarker = createMarker (x, y, z, "cylinder", markersize, 0, 255, 0, 150) -- Creates the markers 
            setElementAlpha ( t, 0 ) 
            setElementCollisionsEnabled(t, false) 
  
        end 
    end 
end 
addEventHandler ( "onResourceStart", getRootElement(), checkforheads ) 
  
-- setTimer ( checkforheads, 1000, 1 ) 
  
  
  
function MarkerColorChange () -- Random Colors for the markers 
allMarkers = getElementsByType ("marker") 
  
  
for _, mrkr in pairs( allMarkers ) do 
        if ( getMarkerSize (mrkr) == markersize ) then 
            setTimer (function() setMarkerColor ( mrkr, math.random (0,255), math.random (0,255), math.random (0,255), 80 ) end, 150, 0 ) 
             
  
        end 
    end 
end 
  
-- setTimer ( MarkerColorChange, 1000, 1 ) 
addEventHandler ( "onResourceStart", getRootElement(), MarkerColorChange ) 
  
  
--------------------HIT FUNCTIONS----------------- 
function MassiveCar () 
    outputChatBox ("MassiveCar Function") 
end 
  
  
function Barrel () 
    outputChatBox ("Barrel Function") 
end 
  
  
function Haybale () 
    outputChatBox ("Haybale Function") 
end 
  
  
function Rocket () 
    outputChatBox("Rocket Function") 
end 
  
-------------------END HIT FUNCTIONS------------- 
  
function hittingthemarker ( hitElement ) 
  
local randomfunctionfromtable = functionstable[math.random(1,#functionstable)] 
    if getElementType( hitElement ) == "player" then 
        if isPlayerInVehicle( hitElement ) then 
                        setTimer (randomfunctionfromtable, 50, 1) 
                         
  
        end 
    end 
end  
addEventHandler('onMarkerHit', getRootElement(), hittingthemarker) 

EDIT:

i rewrote my table to:

functionstable =   { 'MassiveCar', 'Barrel', 'Haybale', 'Rocket' } 
  

Now instead of giving me the error: Expected function at argument 1, got table, it gives me the error "expected function at argument 1, got string 'string' "

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