Jump to content

Help with setTimer


Resto

Recommended Posts

Posted

hello, its my old script and how or where i must add setTimer for 5 seconds lvl refreshed. And when anyone lvl up in chat "Resto" lvl up [lvl]". And when anyone lvl up he get reward 2,000$ for lvl up. Please can anyone help me?

exports.scoreboard:addScoreboardColumn('Level') 
  
addEventHandler("onPlayerSpawn",root, 
function() 
    local account = getPlayerAccount(source) 
    local zombiekills = getAccountData(account,"Zombie kills") 
if (zombiekills >= 0) and (zombiekills <= 50) then 
setAccountData ( account, "Level", "1" ) 
elseif (zombiekills >= 50) and (zombiekills <= 150) then 
setAccountData ( account, "Level", "2" ) 
end 
end 
) 
  
addEventHandler("onPlayerSpawn",root, 
function () 
    local cuenta = getPlayerAccount(source) 
    if isGuestAccount(cuenta) then return end 
    local lvl = getAccountData(cuenta,"Level") 
    if lvl then 
        setElementData(source,"Level", lvl ) 
end 
end 
) 

Posted

Your code only checks kills on spawn. There is no detection of when a player's kills increases.

function died(ammo, killer) 
    -- check if zombie 
    local acc = getPlayerAccount(killer) 
    if (isGuestAccount(acc)) then return end 
    local zombiekills = getAccountData(acc, 'Zombie kills') 
    zombiekills = tonumber(zombiekills) + 1 
end 
addEventHandler("onPedWasted", root, died) --Add the Event when ped1 dies 

Now you just need to check for levels. I would suggest moving the kills to a table, and only set the account's data when the player quits/logs out. Also, remove the same from the account data key.

Posted

Try this one:

  
exports.scoreboard:addScoreboardColumn('Level') 
levelCache = {} 
  
function updateStats(plr) 
    local acc = getPlayerAccount(plr) 
    local accName = getAccountName(acc) 
    local zKills = getAccountData(acc, "Zombie Kills") or 0 
    if zKills => 0 and zKills < 50 then 
        setAccountData(acc, "Level", 1) 
    elseif zKills => 50 and zKills < 150 then 
        setAccountData(acc, "Level", 2) 
    end 
    local lvl = getAccountData(acc, "Level") or 1 
    if lvl > levelCache[accName] then 
        outputChatBox(getPlayerName(plr).." leveled up from "..levelCache[accName].." to "..lvl) 
        givePlayerMoney(plr, 2000) -- Give money on level up. 
    end 
    levelCache[accName] = lvl 
end 
  
function getAllPlayers() 
    local players = getElementsByType("player") 
    for k, v in pairs(players) do 
        updateStats(v) 
    end 
end 
  
setTimer(getAllPlayers, 5000, 0) 

Like JR10 said, you will need to update kills onClientKill or whatever event triggered on killing a zombie.

Posted

@Ryancit2 You never really used setElementData to update the scoreboard. Also, it would be better to use an event instead of using an infinite timer.

Posted
Try this one:
  
exports.scoreboard:addScoreboardColumn('Level') 
levelCache = {} 
  
function updateStats(plr) 
    local acc = getPlayerAccount(plr) 
    local accName = getAccountName(acc) 
    local zKills = getAccountData(acc, "Zombie Kills") or 0 
    if zKills => 0 and zKills < 50 then 
        setAccountData(acc, "Level", 1) 
    elseif zKills => 50 and zKills < 150 then 
        setAccountData(acc, "Level", 2) 
    end 
    local lvl = getAccountData(acc, "Level") or 1 
    if lvl > levelCache[accName] then 
        outputChatBox(getPlayerName(plr).." leveled up from "..levelCache[accName].." to "..lvl) 
        givePlayerMoney(plr, 2000) -- Give money on level up. 
    end 
    levelCache[accName] = lvl 
end 
  
function getAllPlayers() 
    local players = getElementsByType("player") 
    for k, v in pairs(players) do 
        updateStats(v) 
    end 
end 
  
setTimer(getAllPlayers, 5000, 0) 

Like JR10 said, you will need to update kills onClientKill or whatever event triggered on killing a zombie.

ERROR: Loading script failed: [gameplay]/lvl-system/lvlsystem.lua:8: "then" expected near "="

Posted
Line 15 should be:
if lvl > (levelCache[accName] or 0) then 

ERROR: [gameplay]/test/test.lua:15: attempt to concatenate field "?" (a nil value)

But i want only this please: outputChatBox(getPlayerName(plr).." leveled up to "..lvl)

Posted
Post your current code.
exports.scoreboard:addScoreboardColumn('Level') 
levelCache = {} 
  
function updateStats(plr) 
    local acc = getPlayerAccount(plr) 
    local accName = getAccountName(acc) 
    local zKills = getAccountData(acc, "Zombie Kills") or 0 
    if zKills => 0 and zKills < 50 then 
        setAccountData(acc, "Level", 1) 
    elseif zKills => 50 and zKills < 150 then 
        setAccountData(acc, "Level", 2) 
    end 
    local lvl = getAccountData(acc, "Level") or 1 
    if lvl > levelCache[accName] then 
        outputChatBox(getPlayerName(plr).." leveled up from "..levelCache[accName].." to "..lvl) 
        givePlayerMoney(plr, 2000) -- Give money on level up. 
    end 
    levelCache[accName] = lvl 
end 
  
function getAllPlayers() 
    local players = getElementsByType("player") 
    for k, v in pairs(players) do 
        updateStats(v) 
    end 
end 
  
setTimer(getAllPlayers, 5000, 0) 

Posted

This script doesn't really make much sense to me, why would you use a timer to level up? you can use the onAccountDataChange event to detect when "Zombie Kills" data is changed.

local levelCache = { } 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function ( ) 
        exports.scoreboard:addScoreboardColumn ( 'Level' ) 
  
        for _, player in ipairs ( getElementsByType ( "player" ) ) do 
            local account = getPlayerAccount ( player ) 
            if ( not isGuestAccount ( account ) ) then 
                local accName = getAccountName ( account ) 
                levelCache [ accName ] = ( tonumber ( getAccountData ( account, "Level" ) ) or 1 ) 
                setElementData ( player, "Level", levelCache [ accName ] ) 
            end 
        end 
    end 
) 
  
addEventHandler ( "onPlayerLogin", root, 
    function ( _, account ) 
        local accName = getAccountName ( account ) 
        levelCache [ accName ] = ( tonumber ( getAccountData ( account, "Level" ) ) or 1 ) 
        setElementData ( source, "Level", levelCache [ accName ] ) 
    end 
) 
  
addEventHandler ( "onAccountDataChange", root, 
    function ( account, key, value ) 
        local player = getAccountPlayer ( account ) 
        if isElement ( player ) then 
            if ( key == "Zombie Kills" ) then 
                local value = ( tonumber ( value ) and tonumber ( value ) or 0 ) 
                if ( value >= 0 and value < 50 ) then 
                    setAccountData ( account, "Level", 1 ) 
                elseif ( value >= 50 and value < 150 ) then 
                    setAccountData ( account, "Level", 2 ) 
                end 
            elseif ( key == "Level" ) then 
                local accName = getAccountName ( account ) 
                local value = ( tonumber ( value ) and tonumber ( value ) or 0 ) 
                if ( value > ( levelCache [ accName ] or 1 ) ) then 
                    outputChatBox ( getPlayerName ( player ) .." leveled up from ".. tostring ( levelCache [ accName ] or 1 ) .." to ".. tostring ( value ) ) 
                    givePlayerMoney ( player, 2000 ) -- Give money on level up. 
                    levelCache [ accName ] = value 
                    setElementData ( player, "Level", levelCache [ accName ] ) 
                end 
            end 
        end 
    end 
) 

Posted
This script doesn't really make much sense to me, why would you use a timer to level up? you can use the onAccountDataChange event to detect when "Zombie Kills" data is changed.
local levelCache = { } 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function ( ) 
        exports.scoreboard:addScoreboardColumn ( 'Level' ) 
  
        for _, player in ipairs ( getElementsByType ( "player" ) ) do 
            local account = getPlayerAccount ( player ) 
            if ( not isGuestAccount ( account ) ) then 
                local accName = getAccountName ( account ) 
                levelCache [ accName ] = ( tonumber ( getAccountData ( account, "Level" ) ) or 1 ) 
                setElementData ( player, "Level", levelCache [ accName ] ) 
            end 
        end 
    end 
) 
  
addEventHandler ( "onPlayerLogin", root, 
    function ( _, account ) 
        local accName = getAccountName ( account ) 
        levelCache [ accName ] = ( tonumber ( getAccountData ( account, "Level" ) ) or 1 ) 
        setElementData ( source, "Level", levelCache [ accName ] ) 
    end 
) 
  
addEventHandler ( "onAccountDataChange", root, 
    function ( account, key, value ) 
        local player = getAccountPlayer ( account ) 
        if isElement ( player ) then 
            if ( key == "Zombie Kills" ) then 
                local value = ( tonumber ( value ) and tonumber ( value ) or 0 ) 
                if ( value >= 0 and value < 50 ) then 
                    setAccountData ( account, "Level", 1 ) 
                elseif ( value >= 50 and value < 150 ) then 
                    setAccountData ( account, "Level", 2 ) 
                end 
            elseif ( key == "Level" ) then 
                local accName = getAccountName ( account ) 
                local value = ( tonumber ( value ) and tonumber ( value ) or 0 ) 
                if ( value > ( levelCache [ accName ] or 1 ) ) then 
                    outputChatBox ( getPlayerName ( player ) .." leveled up from ".. tostring ( levelCache [ accName ] or 1 ) .." to ".. tostring ( value ) ) 
                    givePlayerMoney ( player, 2000 ) -- Give money on level up. 
                    levelCache [ accName ] = value 
                    setElementData ( player, "Level", levelCache [ accName ] ) 
                end 
            end 
        end 
    end 
) 

not work .. :( can you help me how to install to your exp_system? its good this is shit..

Posted

I tested the code and it works fine, if I set my "Zombie Kills" account data, it levels me up.

Post the script where you are changing the "Zombie Kills" account data.

Posted
I tested the code and it works fine, if I set my "Zombie Kills" account data, it levels me up.

Post the script where you are changing the "Zombie Kills" account data.

What? you mean in scoreboard "Zombie Kills" ?

Posted
No, I mean the script which sets the "Zombie Kills" account data.

this?

And i set my Zombie Kills this: i stopped server delete script start server add script.

Please get me this script install? --> https://community.multitheftauto.com/index.php?p=resources&s=details&id=1253

function addPlayerZombieKills(killer) 
    local account = getPlayerAccount(killer) 
    if isGuestAccount(account) then return end 
    local zombieKills = getAccountData(account,"Zombie kills") 
    if not zombieKills then setAccountData(account,"Zombie kills",0) end 
    setAccountData(account,"Zombie kills",tonumber(zombieKills)+1) 
end 
  
  
addEventHandler("onPlayerLogin",root, 
function () 
    local account = getPlayerAccount(source) 
    if isGuestAccount(account) then return end 
    local zombieKills = getAccountData(account,"Zombie kills") 
    if zombieKills then 
        setElementData(source,"Zombie kills",tostring(zombieKills)) 
    else 
        setElementData(source,"Zombie kills",0)     
  
end 
end) 

Posted (edited)

in this code the data name is Zombie kills and the other codes Zombie Kills, take care of the small/capital letters

Edited by Guest
Posted

My "exp_system" is really easy to use, you can find examples in the wiki page:

https://wiki.multitheftauto.com/wiki/Resource:Exp_system

This is an example of how to give 5 experience points everytime you kill a zombie from slothman's zombies resource:

addEvent ( "onZombieWasted", true ) 
addEventHandler ( "onZombieWasted", root, 
    function ( theKiller ) 
        exports.exp_system:addPlayerEXP ( theKiller, 5 ) 
    end 
) 

Posted
My "exp_system" is really easy to use, you can find examples in the wiki page:

https://wiki.multitheftauto.com/wiki/Resource:Exp_system

This is an example of how to give 5 experience points everytime you kill a zombie from slothman's zombies resource:

addEvent ( "onZombieWasted", true ) 
addEventHandler ( "onZombieWasted", root, 
    function ( theKiller ) 
        exports.exp_system:addPlayerEXP ( theKiller, 5 ) 
    end 
) 

I am with this idiot -_- i add in my server and add in acl.xml and not work..

Posted

Why are you insulting the person that is trying to help you? that's rather stupid.

What has the acl.xml to do with this? it's a simple server side script.

Posted
Why are you insulting the person that is trying to help you? that's rather stupid.

What has the acl.xml to do with this? it's a simple server side script.

I insulting to me -_- i am idiot in this your link.. i see only "congratulation your level is 4! but i dont see this:

Level: Example 4 Experience: 200 > 500 :( <-- this not work

Posted

You mean the progress bar in bottom of your screen that shows your current level and experience and required experience for the next level? if so, then that is not included in the "exp_system" resource, you need to make it yourself.

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