Jump to content

onElementDataChange Help


mremin

Recommended Posts

local rootElement = getRootElement() 
levels = {} 
levels[0] = {"Test 0", "10"} 
levels[1] = {"Test 1", "20"} 
levels[2] = {"Test 2", "100"} 
levels[3] = {"Test 3", "150"} 
levels[4] = {"Test 4", "200"} 
  
function ResourceStart () 
executeSQLCreateTable("levels", "accountname STRING, level INT") 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), ResourceStart) 
  
function addPlayer() 
local account = getPlayerAccount(source) 
local playername = getPlayerName(source) 
CheckPlayer = executeSQLSelect ( "levels", "accountname", "accountname = '" .. getAccountName(account) .. "'" ) 
if ( type( CheckPlayer ) == "table" and #CheckPlayer == 0 ) or not CheckPlayer then  
executeSQLInsert ( "levels", "'".. getAccountName(account) .."','0'" ) 
    end 
end 
addEventHandler("onPlayerLogin",rootElement,addPlayer) 
  
addEventHandler( "onElementDataChange", rootElement,  
function () 
if getElementType(source) == "player" then 
    local acc = getPlayerAccount(source) 
    local accountname = getAccountName(acc) 
    local playername = getPlayerName(source) 
    local data = getElementData(source,"exp") 
    local sqldata = executeSQLSelect ( "levels", "level","accountname = '" .. accountname .. "'") 
    lvl = sqldata[1]["level"] 
    if tostring(data) == tostring(levels[lvl][2]) then 
    outputChatBox("*Level system: congratulations ".. playername .." you're now a ".. levels[lvl][1] .."!",getRootElement(),0,255,0) 
    local textDis = textCreateDisplay () 
    local item = textCreateTextItem( "LEVEL UP! YOU'RE NOW LEVEL ".. tostring(sqldata[1]["level"]), 0.5, 0.4, 2, 0, 255, 0, 255, 4, "center", "center" ) 
    textDisplayAddText ( textDis, item ) 
    textDisplayAddObserver ( textDis, source ) 
    setTimer ( textDestroyTextItem, 5000, 1, item ) 
    setTimer ( textDestroyDisplay, 5000, 1, textDis ) 
    sql = tonumber(sqldata[1]["level"]) + 1 
    executeSQLUpdate("levels", "level = '".. sql .."'", "accountname = '".. accountname .."'")   
       end 
    end 
end) 

-24. Error in row-I could not, please help me

https://community.multitheftauto.com/index.php?p= ... 253&vote=5 <-------the real link here

Link to comment

script doesnt work? and doesnt show the error? ;o

You kill peds or players ? and dont work? this is problem?

edit:

1. You haven't got mysql database

2. You haven't got reward on wasted

  
  
function rewardOnWasted ( ammo, killer, killerweapon, bodypart ) 
if (killer) and (killer ~=source) then 
local exp = getElementData(killer,"exp") 
if exp then 
setElementData(killer,"exp",tonumber(getElementData(killer,"exp"))+5) 
outputChatBox("Experience gained +5! total ".. tonumber(exp)+5,killer,0,255,0) 
else 
setElementData(killer,"exp",0) 
end 
end 
end 
addEventHandler ( "onPlayerWasted", getRootElement(), rewardOnWasted ) 

~by Solidsnake14

Link to comment

dukenukem, this uses sqlite not mysql, the database is created when resource starts and when a player logs-in he will be added with level 1 or 0 (i don't remember) and when the element data changes he will raise up, i made an example of how to use it with onPlayerWasted event and works fine, he may edited it or not using it right.

btw, could mremin show us the code he use to make it work?

Link to comment

Well, someone was asking for a experience/level system in mta forums so i didn't had anything to do and made it :P

(i hope this is what they mean :D)

script works like this:

the script works onElementDataChange that means when you do like: setElementData(playerElement,"exp",10) will raise your level to 1, the script checks for a data in a sql database that is created when resource starts and when a player logins the data will be set to 0 if he doesn't had any data yet.

then the script finds that data and compares it.

P.S: i'm bad in explaining :P

Would you make i could not

Link to comment

use the example of killing,

function rewardOnWasted ( ammo, killer, killerweapon, bodypart ) 
if (killer) and (killer ~=source) then 
local exp = getElementData(killer,"exp") 
if exp then 
setElementData(killer,"exp",tonumber(getElementData(killer,"exp"))+5) 
outputChatBox("Experience gained +5! total ".. tonumber(exp)+5,killer,0,255,0) 
else 
setElementData(killer,"exp",0) 
end 
end 
end 
addEventHandler ( "onPlayerWasted", getRootElement(), rewardOnWasted ) 

now go kill players and you will raise levels..

I think you don't get how it works,

levels[0] = {"Test 0", "10"}

levels[0] = level number

Test 0 = level name or whatever you want example: Level 1

10 = experience required to reach that level

Link to comment

i copied a small code from race_starter_pack and added to set experience on DD/DM win

function DestructionMoney() 
local alivePlayers = getAlivePlayers() 
if info == "Destruction derby" then 
   if #alivePlayers == 1 then 
   local playername = getPlayerName(alivePlayers[1]) 
   outputChatBox("*Wins: "..playername.." took $2500 for surviving!",getRootElement(),255,255,0) 
   setElementData(alivePlayers[1] ,"exp",tonumber(getElementData(alivePlayers[1],"exp"))+5) 
   end  
end 
end 
addEventHandler("onPlayerWasted",gtRootElement(),DestructionMoney) 

try it (not tested)

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