Sign in to follow this  
LOOah

[HELP] need help with exp when i kill player

Recommended Posts

local xpNeed = 100; -- E.g. 100 * lvl = XP you need to rank up!
addEvent("onPlayerLevelUp", true);
addEvent("onZombieGetsKilled", true)

function addPlayerXp(player, xp)
    local acc = getPlayerAccount(player);
    local oldexp = getAccountData(acc, "exp") or 0;
    local oldlvl = getAccountData(acc, "lvl") or 1;
    local newlevel = oldlvl + 1;
    local newexp = oldexp + xp;
    setAccountData(acc, "exp", newexp);
    setElementData(player, "exp", newexp);
    if (newexp >= (oldlvl * xpNeed)) then
        outputChatBox("[Level-Up] Congratulations! New level "..newlevel.."!", player, 66, 134, 244);
        setAccountData(acc, "lvl", newlevel);
        setAccountData(acc, "exp", 0);
        setElementData(player, "exp", 0);
        setElementData(player, "lvl", newlevel);
        triggerEvent("onPlayerLevelUp", player, newlevel);
    end
end

addEventHandler("onPlayerLogin", root, function()
    local acc = getPlayerAccount(source);
    setElementData(source, "lvl", getAccountData(acc, "lvl") or 0);
    setElementData(source, "exp", getAccountData(acc, "exp") or 0);
end);

function rewardOnWasted ( killer ) 
    local exp = getElementData(killer,"exp") 
    local thelvl = getElementData(killer, "lvl") or 0;
    local theexp = getElementData(killer, "exp") or 0;
        if exp then 
        setElementData(killer,"exp",tonumber(getElementData(killer,"exp"))+50) 
        addPlayerXp(killer, 50)
        outputChatBox("Experience gained +50! total ".. tostring(((thelvl + 1) * xpNeed) - theexp -50).."!", killer )
        else 
        setElementData(killer,"exp",50) 
       end 
end 
addEvent("onZombieGetsKilled", true)
addEventHandler("onZombieGetsKilled", getRootElement(), rewardOnWasted)


function rewardOnWasted1 ( killer1)
    --if there is a killer, and that killer is not the same person as whoever died
    if ( killer1 ) and ( killer1 ~= source ) then 
        setElementData(killer1,"exp",tonumber(getElementData(killer1,"exp"))+50) 
        addPlayerXp(killer1, 50)
        outputChatBox("Experience gained +50! total ".. tostring(((thelvl + 1) * xpNeed) - theexp -50))
        else 
        setElementData(killer1,"exp",50) 
    end
end
addEvent("killDayZPlayer", true)
addEventHandler ( "killDayZPlayer", getRootElement(), rewardOnWasted1 ) --attach the rewardOnWasted function to the relevant event

 

Hi, I have a problem when I killing zombies, I get exp, but when I kill players I don't get it, anyone knows what the solution is?

Share this post


Link to post

You misunderstand the using of the event system

Try this, if this is not working reply me with the name of the gamemode you're using.

local xpNeed = 100; -- E.g. 100 * lvl = XP you need to rank up!
addEvent("onPlayerLevelUp", true);
-- addEvent("onZombieGetsKilled", true) you do't need to add the event here as this event is already added in the dayz gamemode

function addPlayerXp(player, xp)
    local acc = getPlayerAccount(player);
    local oldexp = getAccountData(acc, "exp") or 0;
    local oldlvl = getAccountData(acc, "lvl") or 1;
    local newlevel = oldlvl + 1;
    local newexp = oldexp + xp;
    setAccountData(acc, "exp", newexp);
    setElementData(player, "exp", newexp);
    if (newexp >= (oldlvl * xpNeed)) then
        outputChatBox("[Level-Up] Congratulations! New level "..newlevel.."!", player, 66, 134, 244);
        setAccountData(acc, "lvl", newlevel);
        setAccountData(acc, "exp", 0);
        setElementData(player, "exp", 0);
        setElementData(player, "lvl", newlevel);
        triggerEvent("onPlayerLevelUp", player, newlevel);
    end
end

addEventHandler("onPlayerLogin", root, function()
    local acc = getPlayerAccount(source);
    setElementData(source, "lvl", getAccountData(acc, "lvl") or 0);
    setElementData(source, "exp", getAccountData(acc, "exp") or 0);
end);

function rewardOnWasted ( killer ) 
 --   local exp = getElementData(killer,"exp") 
   -- local thelvl = getElementData(killer, "lvl") or 0;
   -- local theexp = getElementData(killer, "exp") or 0;
       -- if exp then 
        --setElementData(killer,"exp",tonumber(getElementData(killer,"exp"))+50) 
        if killer then
            addPlayerXp(killer, 50)
            --outputChatBox("Experience gained +50! total ".. tostring(((thelvl + 1) * xpNeed) - theexp -50).."!", killer ) --you normally don't need this as you have the client syde to show that
        end
       -- else 
       -- setElementData(killer,"exp",50) 
      -- end 
end 
--addEvent("onZombieGetsKilled", true)
addEventHandler("onZombieGetsKilled", getRootElement(), rewardOnWasted)


function rewardOnWasted1 ( killer1)
    --if there is a killer, and that killer is not the same person as whoever died
    if  ( killer1 ) and ( killer1 ~= source ) and getElementType(killer1) == "player" then 
     --   setElementData(killer1,"exp",tonumber(getElementData(killer1,"exp"))+50) 
        addPlayerXp(killer1, 50)
      --  outputChatBox("Experience gained +50! total ".. tostring(((thelvl + 1) * xpNeed) - theexp -50))
        else 
        setElementData(killer1,"exp",50) 
    end
end
-- addEvent("killDayZPlayer", true) same for this one!
addEventHandler ( "killDayZPlayer", getRootElement(), rewardOnWasted1 ) --attach the rewardOnWasted function to the relevant event

You may delete the comments as I let them there just so you know the mistakes you done.

 

Greetings.

  • Confused 1

Share this post


Link to post

I use 0.7 which you once made public, if I post a comment on this 

setElementData(killer1,"exp",tonumber(getElementData(killer1,"exp"))+50)

script does not work, which is ridiculous because I am aware that I do not need it, but without it I do not add XP, and on the client side EXP: it doesn't change, if I do functions with zombies the way I did. It works, and i dont have errors in db, I only have a problem with killing players, because I don't get exp, and could you help me, how can I get exp for players?

sorry for bad english :f

Share this post


Link to post

You tried my way and it didn't work?

Also may I see the errors in /debugscript 3 ?

  • Thanks 1

Share this post


Link to post

there is no bugs in db if im using my script or your but killing player not working, i got exp for zombie but cant get from killing player: https://streamable.com/8q10m4








 

Share this post


Link to post

Try this one:

local xpNeed = 100; -- E.g. 100 * lvl = XP you need to rank up!
addEvent("onPlayerLevelUp", true);

function addPlayerXp(player, xp)
	outputChatBox("3")-- test do not delete
	local acc = getPlayerAccount(player);
	local oldexp = getAccountData(acc, "exp") or 0;
	local oldlvl = getAccountData(acc, "lvl") or 1;
	local newlevel = oldlvl + 1;
	local newexp = oldexp + xp;
	setAccountData(acc, "exp", newexp);
	setElementData(player, "exp", newexp);
	if (newexp >= (oldlvl * xpNeed)) then
		local expleft = newexp - (oldlvl * xpNeed); -- added calculation for the exp that may remains after level up.
		outputChatBox("[Level-Up] Congratulations! New level "..newlevel.."!", player, 66, 134, 244);
		setAccountData(acc, "lvl", newlevel);
		setAccountData(acc, "exp", expleft);
		setElementData(player, "exp", expleft);
		setElementData(player, "lvl", newlevel);
		triggerEvent("onPlayerLevelUp", player, newlevel, oldlvl, oldexp, newexp);
	end
end

addEventHandler("onPlayerLogin", root, function()
	local acc = getPlayerAccount(source);
	if acc then
		setElementData(source, "lvl", getAccountData(acc, "lvl") or 0);
		setElementData(source, "exp", getAccountData(acc, "exp") or 0);
	end
end);

addEventHandler("onZombieGetsKilled", root, function(killer) 
	if killer and getElementType(killer) == "player" then
		addPlayerXp(killer, 50);
	end
end);

addEventHandler("killDayZPlayer", root, function(killer)
	outputChatBox("1") -- test do not delete
	if (killer and killer ~= source and getElementType(killer) == "player") then
		outputChatBox("2")-- test do not delete
		addPlayerXp(killer, 50);
	end
end);

and make another video

  • Thanks 1

Share this post


Link to post

You misspelled the 'kilLDayZPlayer' event as you can see it's kilL and not kill (don't know why)

local xpNeed = 100; -- E.g. 100 * lvl = XP you need to rank up!
addEvent("onPlayerLevelUp", true);

function addPlayerXp(player, xp)
	local acc = getPlayerAccount(player);
	local oldexp = getAccountData(acc, "exp") or 0;
	local oldlvl = getAccountData(acc, "lvl") or 1;
	local newlevel = oldlvl + 1;
	local newexp = oldexp + xp;
	setAccountData(acc, "exp", newexp);
	setElementData(player, "exp", newexp);
	if (newexp >= (oldlvl * xpNeed)) then
		local expleft = newexp - (oldlvl * xpNeed); -- added calculation for the exp that may remains after level up.
		outputChatBox("[Level-Up] Congratulations! New level "..newlevel.."!", player, 66, 134, 244);
		setAccountData(acc, "lvl", newlevel);
		setAccountData(acc, "exp", expleft);
		setElementData(player, "exp", expleft);
		setElementData(player, "lvl", newlevel);
		triggerEvent("onPlayerLevelUp", player, newlevel, oldlvl, oldexp, newexp);
	end
end

addEventHandler("onPlayerLogin", root, function()
	local acc = getPlayerAccount(source);
	if acc then
		setElementData(source, "lvl", getAccountData(acc, "lvl") or 0);
		setElementData(source, "exp", getAccountData(acc, "exp") or 0);
	end
end);

addEventHandler("onZombieGetsKilled", root, function(killer) 
	if killer and getElementType(killer) == "player" then
		addPlayerXp(killer, 50);
	end
end);

addEventHandler("kilLDayZPlayer", root, function(killer)
	if (killer and killer ~= source and getElementType(killer) == "player") then
		addPlayerXp(killer, 50);
	end
end);

You're welcome

  • Like 1

Share this post


Link to post
Posted (edited)

thanks i appreciate your help 

and last question, what function or event i need to use to do like max 30levels and award for hitting level, like if you hit level 5 you will get knife on respawn

Edited by LOOah

Share this post


Link to post

That's pretty simple as you may just add a if oldlvl + 1 > 30 then return end within addPlayerXp function.

And for rewards using the event onPlayerLevelUp you add statement like if newlvl >= 1 then give him something.

And for items at spawn you sould check his lvl on spawn and give the item depending on his level

Sorry I'm on the phone so I can't do better :/

 

 

  • Thanks 1

Share this post


Link to post

Great, i will try to do this, cheers. 

 

Share this post


Link to post
Posted (edited)

 

So, I was able to block the experience when you reach level 15, but I have a really big problem creating a function that will give you an item to start if you have enough level

i was  trying like that ( dont laught i know its bad )

 

if getElementData(player,'lvl') >= 9 then
itemName == ("Milk", 1)


 

addEventHandler ("onPlayerSpawn", getRootElement(), 
function()
    if getElementData(player,'lvl') > 9 then 
        setElementData("Milk",1 )

 

7 hours ago, Tekken said:

 

And for rewards using the event onPlayerLevelUp you add statement like if newlvl >= 1 then give him something.

And for items at spawn you sould check his lvl on spawn and give the item depending on his level

 

 

 

help

Edited by LOOah

Share this post


Link to post

So in accounts.Lua you should have an event called onPlayerDayZLogin

addEventHandler("onPlayerDayZLogin", root, function()
	local lvl = getElementData(source, "lvl") or 0;
	if lvl then
		if lvl > 1 then
			-- give him something
		elseif lvl > 2 then
			-- give him something
		elseif lvl > 3 then
			-- give him something
		elseif lvl > 4 then
			-- give him something
		elseif lvl > 5 then
			-- give him something
		end
	end
end);

And for  onPlayerLevelUp

addEventHandler("onPlayerLevelUp", root, function(newlvl)
	if newlvl then
		if newlvl > 1 then
			-- give him something
		elseif newlvl > 2 then
			-- give him something
		elseif newlvl > 3 then
			-- give him something
		elseif newlvl > 4 then
			-- give him something
		elseif newlvl > 5 then
			-- give him something
		end
	end
end);

This is pretty rudimentary as I highly recommend u using something like this but you may not understand

table = {"Milk","GPS","Map","Milk","Milk","Milk","Milk","Milk","Milk","Milk","Milk","Milk","Milk","Milk","Milk"} --15 items for 15 levels (you got it?)

addEventHandler("EVENT-NAME-HERE", root, function(lvl) -- You may add the event you want here
	if not lvl then lvl = getElementData(source, "lvl") or 0; end
	if lvl >= 1 then
		for i = 1, lvl do
			setElementData(source, table[i], getElementData(source, table[i]) + 1);
		end
	end
end);

good luck

Share this post


Link to post

 

wow, lots of information, I will try to comprehend something and create something when I wake up, all night I tried to do it and now I go to sleep, If i will have problems i will write here , thanks again, I should get up around 6, cheers. 

Share this post


Link to post
Posted (edited)
14 hours ago, Tekken said:

So in accounts.Lua you should have an event called onPlayerDayZLogin

And for  onPlayerLevelUp

This is pretty rudimentary as I highly recommend u using something like this but you may not understand

good luck

so i was trying like this but none of this was working 
in accounts.Lua i was trying this option ( not everything at once ) 
 

addEventHandler("onPlayerDayZLogin", root, function()
    local lvl = getElementData(source, "lvl") or 0;
    if lvl then
        if lvl >= 1 then
            setElementData(player, "MP5A5", 1)
        elseif lvl >= 2 then
            setElementData(player, "Tent", 1)
        elseif lvl >= 3 then
            setElementData(player, "Milk", 1)
        elseif lvl >= 4 then
            setElementData(player, "Medic Kit", 1)
        elseif lvl >= 5 then
            setElementData(player, "Shovel", 1)
        end
    end
end);
table = {"Milk","MP5A5","M4A1-S","AK-47","Soda Bottle","Pizza","AS50","Tent","Medium Tent","ACR","AR-15","M107","Pasta Can","Beans Can","Golf Club"} 

addEventHandler("onPlayerDayZLogin", root, function(lvl) 
    if not lvl then lvl = getElementData(source, "lvl") or 0; end
    if lvl >= 1 then
        for i = 1, lvl do
            setElementData(source, table[i], getElementData(source, table[i]) + 1);
        end
    end
end);
addEventHandler("onPlayerDayZLogin", root, function()
    local lvl = getElementData(source, "lvl") or 0;
    if lvl then
        if lvl >= 1 then
            setElementData("M4A1-S", 1)
        end
    end

end);
addEventHandler("onPlayerSpawn", root, function()
    local lvl = getElementData(source, "lvl") or 0;
    if lvl then
        if lvl > 1 then
            setElementData(source, "M4A1-S", 1)
        end
    end
end);

AND ON SERVER.Lua IN SCRIPT LEVEL I WAS TRYING THIS 

 

addEventHandler("onPlayerLevelUp", root, function(newlvl)
    if newlvl then
    if lvl >= 1 then
            setElementData(player, "MP5A5", 1)
        elseif lvl >= 2 then
            setElementData(player, "Tent", 1)
        elseif lvl >= 3 then
            setElementData(player, "Milk", 1)
        elseif lvl >= 4 then
            setElementData(player, "Medic Kit", 1)
        elseif lvl >= 5 then
            setElementData(player, "Shovel", 1)
        end
    end
end);

and none of this work, im just stupid i think... 

EDIT : i had no erros in DB 

Edited by LOOah

Share this post


Link to post

even i was trying like this 
 

addEventHandler("onPlayerLogin", getRootElement(), function()
local level = getAccountData(getPlayerAccount(source), "lvl") or 0
if not level then return outputChatBox(" wrong lvl", source) end
    if level >= 1 then
            setElementData(source, "MP5A5", 1)
        elseif level >= 2 then
            setElementData(source, "Tent", 1)
        elseif level >= 3 then
            setElementData(source, "Milk", 1)
        elseif level >= 4 then
            setElementData(source, "Medic Kit", 1)
        elseif level >= 5 then
            setElementData(source, "Shovel", 1)
        end
end);

and function to test ElementData 

 

function test(plr)
local data = getElementData(plr, "Tent") or 0
outputChatBox (data, plr)
end
addCommandHandler("spr", test)

and when i will use SPR i will have 0 on chat, soi dont know what im doing wrong with that 

Share this post


Link to post

and i now trying do this like that 


 

table = {"Milk","MP5A5","M4A1-S","AK-47","Soda Bottle","Pizza","AS50","Tent","Medium Tent","ACR","AR-15","M107","Pasta Can","Beans Can","Golf Club"} 

addEventHandler("onPlayerSpawn", root, function(lvl)
local player = source 
    if not lvl then lvl = getElementData(player, "lvl") or 0; end
    if lvl >= 1 then
        for i = 1, lvl do
            setElementData(player, "lvl", table[i], (getElementData(player,"lvl", table[i]) or 0) + 1);
        end
    end
end);

they're no errors, but still i dont have weapon and anyting ...

Share this post


Link to post
Posted (edited)

my bad there is an error in line 8 on that setElementData

https://prnt.sc/scajbc
sorry for spamming i cant edit my post :/

Edited by LOOah

Share this post


Link to post
Posted (edited)

This is not how it should line 8 replace this

setElementData(player, "lvl", table[i], (getElementData(player,"lvl", table[i]) or 0) + 1);

with this 

 

setElementData(player, table[i], (getElementData(player, table[i]) or 0) + 1);

As I have showed you in the post before, and as I said if you don't understand you may use the more rudimentary way for the beginning and upgrade it as you learn more, as I have did when I have started learning. Good luck. ;) 

Edited by Tekken
  • Thanks 1

Share this post


Link to post

This first way is easier, I added to accounts.Lua and everything is working, thanks

 

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.