Jump to content

About Tables


Agon

Recommended Posts

Hi. i want to give player random numbers from the table i prepared. How to do it? I know i should use math.random but how? math.random(v) ?

And second question is; is [if getAccountData(source) == "player.one" then] true? If it isn't is there a way to do it?

Hope you understand me :)

numbers ={'1';'2';'3';'4';'5';} 
  
local pickup = createPickup(0,0,5, pickupid) 
local marker = createMarker ( 2, 0, 5, "cylinder", 1, 255, 0, 0, 170 ) 
local account = getPlayerAccount(source) 
  
function giveNumber(source) 
for i,v in pairs(number) do 
-- give random numbers code here i think. After that (you can see the script below) the script will check which number was given to the player. If the number is 1, it will give 10 $ for example. 
if v == '1' then 
setAccountData(account, "player.one", playerone) 
elseif v == '2' then 
setAccountData(account, "player.two", playertwo) 
blah 
blah 
addEventHandler("onPickupHit", pickup, giveNumber) 
  
function giveMySalary(source) 
if (getAccountData(account) == "player.one") then 
giveMoney(source, 10) 
elseif (getAccountData(account) == "player.two") then 
giveMoney(source, 20) 
blah 
blah 
blah 
addEventHandler("onMarkerHit", marker, giveMySalary) 

Link to comment

For the random number you should do something like this:

table[math.random(1, #table)] --WHERE table IS THE NAME OF THE TABLE YOU WANT GET THE NUMBER 

For the sencond question, i understand that: If the selected account have the data "player.one" is true it's ok, if not you will add "player.one" data to this account... if it's right something like this:

if not (getAccountData(account) == "player.one") then 
   setAccountData(account, "player.one", true) 
end 

Link to comment
For the random number you should do something like this:
table[math.random(1, #table)] --WHERE table IS THE NAME OF THE TABLE YOU WANT GET THE NUMBER 

You don't need that '1'.

table[math.random(#table)] --WHERE table IS THE NAME OF THE TABLE YOU WANT GET THE NUMBER 

That would do the same.

Link to comment
local numbers ={'1','2','3','4','5'} 
  
local pickup = createPickup(0,0,5, pickupid) 
local marker = createMarker ( 2, 0, 5, "cylinder", 1, 255, 0, 0, 170 ) 
  
function giveNumber(player) 
     local account = getPlayerAccount(player) 
             local rand = numbers[math.random(#numbers)] 
               if rand == '1' then 
                       setAccountData(player, "player", 'One') 
               elseif rand == '2' then 
                       setAccountData(player, "player", 'Two')  
               end 
        end 
end 
addEventHandler("onPickupHit", pickup, giveNumber) 
  
function giveMySalary(player) 
            local account = getPlayerAccount(player) 
                if (getAccountData(account,"player") == "One") then 
                      giveMoney(player, 10) 
                elseif (getAccountData(account,"player") == "Two") then  
                      giveMoney(player, 20) 
          end 
end 
addEventHandler("onMarkerHit", marker, giveMySalary) 

Is this?

Link to comment
local numbers ={'1','2','3','4','5'} 
  
local pickup = createPickup(0,0,5, pickupid) 
local marker = createMarker ( 2, 0, 5, "cylinder", 1, 255, 0, 0, 170 ) 
  
function giveNumber(player) 
    local account = getPlayerAccount(player) 
    local rand = numbers[math.random(#numbers)] 
    if (rand == '1') then 
        setAccountData(account, "player", 'One') 
    elseif (rand == '2') then 
        setAccountData(account, "player", 'Two') 
    end 
end 
addEventHandler("onPickupHit", pickup, giveNumber) 
  
function giveMySalary(player) 
    local account = getPlayerAccount(player) 
    if (getAccountData(account,"player") == "One") then 
        giveMoney(player, 10) 
    elseif (getAccountData(account,"player") == "Two") then 
        giveMoney(player, 20) 
    end 
end 
addEventHandler("onMarkerHit", marker, giveMySalary) 

Link to comment
local numbers ={'1','2','3','4','5'} 
  
local pickup = createPickup(0,0,5, pickupid) 
local marker = createMarker ( 2, 0, 5, "cylinder", 1, 255, 0, 0, 170 ) 
  
function giveNumber(player) 
    local account = getPlayerAccount(player) 
    local rand = numbers[math.random(#numbers)] 
    if (rand == '1') then 
        setAccountData(account, "player", 'One') 
    elseif (rand == '2') then 
        setAccountData(account, "player", 'Two') 
    end 
end 
addEventHandler("onPickupHit", pickup, giveNumber) 
  
function giveMySalary(player) 
    local account = getPlayerAccount(player) 
    if (getAccountData(account,"player") == "One") then 
        giveMoney(player, 10) 
    elseif (getAccountData(account,"player") == "Two") then 
        giveMoney(player, 20) 
    end 
end 
addEventHandler("onMarkerHit", marker, giveMySalary) 

Ops :lol:

Link to comment

Oh thank you guys. Two more questions. 1) for example i have 20 pickups (for another script). When i get one pickup, i want to remove it. Should i use table again? Because making 20 functions is so difficult...

2) i want to give 10000 $ if the player get all the pickups. Should i use setAccountData? I mean i can make it if the player get first pickup, set his account data "player.first". But you know players can find the pickups randomly. So if player finds last pickup as a chance, it will get 10000$ and i dont want it to happen. He should get all the pickups. What should i do then? Just give me some tips or examples..

Link to comment

For first:

  
addEventHandler('onPickupHit',root, 
      function() 
             if source == marker1 then 
          --something 
             elseif source == marker2 then 
          --something 
          ...... 
          end 
    end 
) 

For second: Yes, you use use setAccountData e.g:

if source == marker1 then 
     local getMarkers = getAccountData(yourAccount,'Markers') 
     setAccountData(yourAccount,'Markers',getMarkers+1) 
  

setTimer ( 
    function() 
        local getMakers = getAccountData(yourAccount,'Markers') 
        if getMakers == '20' then 
            givePlayerMoney(source,10000) 
            setAccountData(yourAccount,'GotMarkers','player.first') 
    end 
end,500,0) 
  
--I THINK THAT IT'S WRONG, SORRY. 
  

It's only examples :S

Link to comment
Thanks anyway. Your second answer will help me but still want an easy way to question 1 :D

No problem :)Your second answer will help me but still want an easy way to question 1I don't know another way, the bether you can do is wait for a more experienced scripter :D

Link to comment

2.answer.

You need create 100 or something count pickups.

Then if player hit pickup check ( if player not hit this pickup ) .

Or better this: if player login , just create pickup which player not hit.( if player not hit just not create this pickups )

You can use for this:

function table.serialize( t ) 
    if not t or type( t ) ~= 'table' then  
        return false 
    end -- if nil or not a table 
    local buf = '{' 
    for key,value in pairs( t ) do 
        local v_type,k_type = type( value ),type( key ) 
        if      v_type ~= 'userdata'    and k_type ~= 'userdata' -- ignore fields and keys witch contain userdata, thread or function 
        and     v_type ~= 'thread'      and k_type ~= 'thread'  
        and     v_type ~= 'function'    and k_type ~= 'function'      
        then 
            if k_type == 'number' then   
                buf = buf .. '['..key..'] = ' 
            else 
                buf = buf .. '[\''..key..'\'] = ' end 
            if v_type == 'table' then  
                value = table.serialize( value )  
            elseif v_type == 'string' then  
                value = '\''..value..'\'' 
            else  
                value = tostring( value )  
            end 
            buf = buf .. value 
            if next( t,key ) then buf = buf..',' end 
        end 
    end 
    return buf .. '}' 
end 

and this

function table.deserialize( s ) 
    if type( s ) == 'string' then 
        local getTableFromString = loadstring( 'return '..s ) 
        local t = getTableFromString( ) 
        if type( t ) ~= 'table' then return false end 
        return t 
    end 
    return false     
end 

Syntax and etc you can see here: viewtopic.php?f=141&t=33091

Link to comment

I want to create all the pickups. For example i have 2 pickups one of them is on chiliad, and the other is in groove street. The player wont be able to see the pickup on the chiliad until he gets the pickup in the groove street with your code. And the players will have to search all the san andreas map everytime.

Link to comment

I get no errors, no output messages, no drugs. what's wrong with this script?

local numbers ={'1','2','3','4','5'} 
local myPlayer = getLocalPlayer() 
  
local drugPickupBlip = createBlip ( -1145, -1134, 130, 46, 1, 0, 0, 0, 255, 0, 99999.0 ) 
local drugPickup = createMarker( -1145, -1134 ,129, "cylinder", 1, 0, 0, 0, 255 ) 
local drugMarkerBlip = createBlip ( -2179, 715.5, 53, 24, 1, 0, 0, 0, 255, 0, 99999.0 ) 
local drugMarker = createMarker( -2180, 715.5, 53, "cylinder", 1, 255, 0, 0, 170 ) 
local drugSeller = createPed( 29, -2177, 715.5, 55, 90 ) 
  
function drugSellerAnim() 
setElementFrozen(drugSeller, true) 
setPedAnimation(drugSeller, "cop_ambient", "Coplook_loop",-1,true,false,false) 
end 
setTimer(drugSellerAnim, 1000, 1) 
  
function drugSellerImmortal ( attacker ) 
    cancelEvent() 
end 
addEventHandler( "onClientPedDamage", getRootElement(), drugSellerImmortal ) 
  
function giveNumber(myPlayer) 
    local account = getPlayerAccount(myPlayer) 
    local rand = numbers[math.random(#numbers)] 
    if (rand == '1') then 
        setAccountData(account, "myPlayer", 'One') 
        outputChatBox("You have found 10 weeds. Go and sell them to a drug seller!", myPlayer, 255, 0, 0, true) 
    elseif (rand == '2') then 
        setAccountData(account, "myPlayer", 'Two') 
        outputChatBox("You have found 20 weeds. Go and sell them to a drug seller!", myPlayer, 255, 0, 0, true) 
    elseif (rand == '3') then 
        setAccountData(account, "myPlayer", 'Three') 
        outputChatBox("You have found 30 weeds. Go and sell them to a drug seller!", myPlayer, 255, 0, 0, true) 
    elseif (rand == '4') then 
        setAccountData(account, "myPlayer", 'Four') 
        outputChatBox("You have found 40 weeds. Go and sell them to a drug seller!", myPlayer, 255, 0, 0, true) 
    elseif (rand == '5') then 
        setAccountData(account, "myPlayer", 'Five') 
        outputChatBox("You have found 50 weeds. Go and sell them to a drug seller!", myPlayer, 255, 0, 0, true) 
    end 
end 
addEventHandler("onMarkerHit", drugPickup, giveNumber) 
  
function giveMySalary(myPlayer) 
    local account = getPlayerAccount(myPlayer) 
    if (getAccountData(account,"myPlayer") == "One") then 
        giveMoney(myPlayer, 100) 
        setAccountData(account, "myPlayer", 'Zero') 
        outputChatBox("There is just 1 drug. Here is your money. Don't forget, more drugs, more money!", myPlayer, 255, 0, 0, true) 
    elseif (getAccountData(account,"myPlayer") == "Two") then 
        giveMoney(myPlayer, 150) 
        setAccountData(account, "myPlayer", 'Zero') 
        outputChatBox("There are 2 drugs. Here is your money. Don't forget, more drugs, more money!", myPlayer, 255, 0, 0, true) 
    elseif (getAccountData(account,"myPlayer") == "Three") then 
        giveMoney(myPlayer, 200) 
        setAccountData(account, "myPlayer", 'Zero') 
        outputChatBox("There are 3 drugs. Here is your money. Don't forget, more drugs, more money!", myPlayer, 255, 0, 0, true) 
    elseif (getAccountData(account,"myPlayer") == "Four") then 
        giveMoney(myPlayer, 250) 
        setAccountData(account, "myPlayer", 'Zero') 
        outputChatBox("There are 4 drugs. Here is your money. Don't forget, more drugs, more money!", myPlayer, 255, 0, 0, true) 
    elseif (getAccountData(account,"myPlayer") == "Five") then 
        giveMoney(myPlayer, 300) 
        setAccountData(account, "myPlayer", 'Zero') 
        outputChatBox("There are 5 drugs. Here is your money. Don't forget, more drugs, more money!", myPlayer, 255, 0, 0, true) 
    elseif (getAccountData(account,"myPlayer") == "Zero") then 
        outputChatBox("It seems you don't have any weed on you. Go and find some.", myPlayer, 255, 0, 0, true) 
    end 
end 
addEventHandler("onMarkerHit", drugMarker, giveMySalary) 

Link to comment

You are using getLocalPlayer in server-side.

Remember, source in onMarkerHit event is the marker that the ped has hitted.

local numbers ={'1','2','3','4','5'} 
      
local drugPickupBlip = createBlip ( -1145, -1134, 130, 46, 1, 0, 0, 0, 255, 0, 99999.0 ) 
local drugPickup = createMarker( -1145, -1134 ,129, "cylinder", 1, 0, 0, 0, 255 ) 
local drugMarkerBlip = createBlip ( -2179, 715.5, 53, 24, 1, 0, 0, 0, 255, 0, 99999.0 ) 
local drugMarker = createMarker( -2180, 715.5, 53, "cylinder", 1, 255, 0, 0, 170 ) 
local drugSeller = createPed( 29, -2177, 715.5, 55, 90 ) 
      
function drugSellerAnim(drugSeller) 
    setElementFrozen(drugSeller, true) 
    setPedAnimation(drugSeller, "cop_ambient", "Coplook_loop",-1,true,false,false) 
end 
setTimer(drugSellerAnim, 1000, 1) 
      
function drugSellerImmortal () 
        cancelEvent() 
end 
addEventHandler( "onClientPedDamage", root, drugSellerImmortal ) 
      
function giveNumber(thePlayer) 
      local account = getPlayerAccount(thePlayer) 
      local rand = numbers[math.random(#numbers)] 
        if (rand == '1') then 
            setAccountData(account, "myPlayer", 'One') 
            outputChatBox("You have found 10 weeds. Go and sell them to a drug seller!", thePlayer, 255, 0, 0, true) 
        elseif (rand == '2') then 
            setAccountData(account, "myPlayer", 'Two') 
            outputChatBox("You have found 20 weeds. Go and sell them to a drug seller!", thePlayer, 255, 0, 0, true) 
        elseif (rand == '3') then 
            setAccountData(account, "myPlayer", 'Three') 
            outputChatBox("You have found 30 weeds. Go and sell them to a drug seller!", thePlayer, 255, 0, 0, true) 
        elseif (rand == '4') then 
            setAccountData(account, "myPlayer", 'Four') 
            outputChatBox("You have found 40 weeds. Go and sell them to a drug seller!", thePlayer, 255, 0, 0, true) 
        elseif (rand == '5') then 
            setAccountData(account, "myPlayer", 'Five') 
            outputChatBox("You have found 50 weeds. Go and sell them to a drug seller!", thePlayer, 255, 0, 0, true) 
        end 
end 
addEventHandler("onMarkerHit", drugPickup, giveNumber) 
      
function giveMySalary(thePlayer) 
      local account = getPlayerAccount(thePlayer) 
        if (getAccountData(account,"myPlayer") == "One") then 
            giveMoney(thePlayer, 100) 
            setAccountData(account, "myPlayer", 'Zero') 
            outputChatBox("There is just 1 drug. Here is your money. Don't forget, more drugs, more money!", thePlayer, 255, 0, 0, true) 
        elseif (getAccountData(account,"myPlayer") == "Two") then 
            giveMoney(thePlayer, 150) 
            setAccountData(account, "myPlayer", 'Zero') 
            outputChatBox("There are 2 drugs. Here is your money. Don't forget, more drugs, more money!", thePlayer, 255, 0, 0, true) 
        elseif (getAccountData(account,"myPlayer") == "Three") then 
            giveMoney(thePlayer, 200) 
            setAccountData(account, "myPlayer", 'Zero') 
            outputChatBox("There are 3 drugs. Here is your money. Don't forget, more drugs, more money!", thePlayer, 255, 0, 0, true) 
        elseif (getAccountData(account,"myPlayer") == "Four") then 
            giveMoney(thePlayer, 250) 
            setAccountData(account, "myPlayer", 'Zero') 
            outputChatBox("There are 4 drugs. Here is your money. Don't forget, more drugs, more money!", thePlayer, 255, 0, 0, true) 
        elseif (getAccountData(account,"myPlayer") == "Five") then 
            giveMoney(thePlayer, 300) 
            setAccountData(account, "myPlayer", 'Zero') 
            outputChatBox("There are 5 drugs. Here is your money. Don't forget, more drugs, more money!", thePlayer, 255, 0, 0, true) 
        elseif (getAccountData(account,"myPlayer") == "Zero") then 
            outputChatBox("It seems you don't have any weed on you. Go and find some.", thePlayer, 255, 0, 0, true) 
        end 
end 
addEventHandler("onMarkerHit", drugMarker, giveMySalary) 

Link to comment

setPedAnimation wrong: https://wiki.multitheftauto.com/wiki/SetPedAnimation

I don't see any setPedFrozen in the script lol :S

    local numbers ={'1','2','3','4','5'} 
          
    local drugPickupBlip = createBlip ( -1145, -1134, 130, 46, 1, 0, 0, 0, 255, 0, 99999.0 ) 
    local drugPickup = createMarker( -1145, -1134 ,129, "cylinder", 1, 0, 0, 0, 255 ) 
    local drugMarkerBlip = createBlip ( -2179, 715.5, 53, 24, 1, 0, 0, 0, 255, 0, 99999.0 ) 
    local drugMarker = createMarker( -2180, 715.5, 53, "cylinder", 1, 255, 0, 0, 170 ) 
    local drugSeller = createPed( 29, -2177, 715.5, 55, 90 ) 
          
    function drugSellerAnim() 
        setElementFrozen(source, true) 
        setPedAnimation(source, "cop_ambient", "Coplook_loop",-1,true,false,false) 
    end 
    setTimer(drugSellerAnim, 1000, 1) 
          
    function drugSellerImmortal () 
            cancelEvent() 
    end 
    addEventHandler( "onClientPedDamage", root, drugSellerImmortal ) 
          
    function giveNumber(thePlayer) 
          local account = getPlayerAccount(thePlayer) 
          local rand = numbers[math.random(#numbers)] 
            if (rand == '1') then 
                setAccountData(account, "myPlayer", 'One') 
                outputChatBox("You have found 10 weeds. Go and sell them to a drug seller!", thePlayer, 255, 0, 0, true) 
            elseif (rand == '2') then 
                setAccountData(account, "myPlayer", 'Two') 
                outputChatBox("You have found 20 weeds. Go and sell them to a drug seller!", thePlayer, 255, 0, 0, true) 
            elseif (rand == '3') then 
                setAccountData(account, "myPlayer", 'Three') 
                outputChatBox("You have found 30 weeds. Go and sell them to a drug seller!", thePlayer, 255, 0, 0, true) 
            elseif (rand == '4') then 
                setAccountData(account, "myPlayer", 'Four') 
                outputChatBox("You have found 40 weeds. Go and sell them to a drug seller!", thePlayer, 255, 0, 0, true) 
            elseif (rand == '5') then 
                setAccountData(account, "myPlayer", 'Five') 
                outputChatBox("You have found 50 weeds. Go and sell them to a drug seller!", thePlayer, 255, 0, 0, true) 
            end 
    end 
    addEventHandler("onMarkerHit", drugPickup, giveNumber) 
          
    function giveMySalary(thePlayer) 
          local account = getPlayerAccount(thePlayer) 
            if (getAccountData(account,"myPlayer") == "One") then 
                giveMoney(thePlayer, 100) 
                setAccountData(account, "myPlayer", 'Zero') 
                outputChatBox("There is just 1 drug. Here is your money. Don't forget, more drugs, more money!", thePlayer, 255, 0, 0, true) 
            elseif (getAccountData(account,"myPlayer") == "Two") then 
                giveMoney(thePlayer, 150) 
                setAccountData(account, "myPlayer", 'Zero') 
                outputChatBox("There are 2 drugs. Here is your money. Don't forget, more drugs, more money!", thePlayer, 255, 0, 0, true) 
            elseif (getAccountData(account,"myPlayer") == "Three") then 
                giveMoney(thePlayer, 200) 
                setAccountData(account, "myPlayer", 'Zero') 
                outputChatBox("There are 3 drugs. Here is your money. Don't forget, more drugs, more money!", thePlayer, 255, 0, 0, true) 
            elseif (getAccountData(account,"myPlayer") == "Four") then 
                giveMoney(thePlayer, 250) 
                setAccountData(account, "myPlayer", 'Zero') 
                outputChatBox("There are 4 drugs. Here is your money. Don't forget, more drugs, more money!", thePlayer, 255, 0, 0, true) 
            elseif (getAccountData(account,"myPlayer") == "Five") then 
                giveMoney(thePlayer, 300) 
                setAccountData(account, "myPlayer", 'Zero') 
                outputChatBox("There are 5 drugs. Here is your money. Don't forget, more drugs, more money!", thePlayer, 255, 0, 0, true) 
            elseif (getAccountData(account,"myPlayer") == "Zero") then 
                outputChatBox("It seems you don't have any weed on you. Go and find some.", thePlayer, 255, 0, 0, true) 
            end 
    end 
    addEventHandler("onMarkerHit", drugMarker, giveMySalary) 

Try.

Link to comment

It has nothing to do with that, the problem is here:

function drugSellerAnim() 
    setElementFrozen(source, true) 
    setPedAnimation(source, "cop_ambient", "Coplook_loop",-1,true,false,false) 
end 
setTimer(drugSellerAnim, 1000, 1) 

Where is 'source' defined here?

Link to comment
It has nothing to do with that, the problem is here:
function drugSellerAnim() 
    setElementFrozen(source, true) 
    setPedAnimation(source, "cop_ambient", "Coplook_loop",-1,true,false,false) 
end 
setTimer(drugSellerAnim, 1000, 1) 

Where is 'source' defined here?

I thought the problem was that too, but I'm a big dumb in these things '-' :(

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