Get XP from zombies!

hey guys , i have a problem with this code ...

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(500)
		local x,y,z = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
		local nearby = {}
		for _,ped in pairs(GetNearbyPeds(x,y,z,100.0)) do
			if DoesEntityExist(ped) and IsPedDeadOrDying(ped, 1) then
				if GetPedSourceOfDeath(ped) == GetPlayerPed(-1) then
					exports.XNLRankBar:Exp_XNL_AddPlayerXP(100)
					TriggerServerEvent('AddXp', 100)
				end
			end
		end
	end
end)

i am making a zombie server and i can't figure out how to make players gain xp from killing zombies 
( i have xnlrank bar in my server which is working perfectly with these two lines that make players earn xp )            
                      
 exports.XNLRankBar:Exp_XNL_AddPlayerXP(100)
 TriggerServerEvent('AddXp', 100)

I get an error "attempt to call a nil value ( global 'GetNearbyPeds'  ) can you fix the code guys ?
um i also tried using this , i don't get errors and everything is fine but the xp and money are not coming .


 Citizen.CreateThread(function ()
	while true do
		Citizen.Wait(1000)
		local playerPed = PlayerPedId()
		local targetPed = Citizen.InvokeNative(GetHashKey("u_m_y_zombie_01"), PlayerId(), Citizen.ReturnResultAnyway())		if HasEntityBeenDamagedByEntity(targetPed, playerPed, 1) then
			if DoesEntityExist(targetPed) and IsPedDeadOrDying(targetPed, 1) then
				if GetPedSourceOfDeath(targetPed) == GetPlayerPed(-1) then
					exports.XNLRankBar:Exp_XNL_AddPlayerXP(100)
				TriggerServerEvent('AddXp', 100)
					local xPlayer = ESX.GetPlayerFromId(source)
					xPlayer.addMoney(900)				end
			end
		end
	end
end)

all of this is not working -> i found a way to make things work -> check the solution

well there is no native called GetNearbyPeds
you can try implementing it using this vRP2 code

1 Like

thanks but the vrp code you sent is about players not peds .
i can do it easly for players but for peds i can’t can you explain further ?

nvm it was very easy with esx_zombiesystem and xnlrankbar , i was just too dumb .

how do you solve it?
can you share the solution with me?

are you using xnlrankbar and esx_zombiesystem ?

iam using esx_xp and esx_zombiesystem.

when I input another function after the zombie dies the function will work repeatedly.

for example: I put the add xp function or just a notification after the zombie dies, the function will continue without stopping until the server goes down.

I really appreciate your help

i don’t really know about esx_xp never used it however i use a similar ressource called xnlrankbar ( i think it’s better but it’s hard to setup , well the hud of both scripts look the same since they both use gta o style however the one i use doesn’t have a leaderboard ) .
my esx_zombiesystem \client\main.lua line 526 looks like this :

										if randomChance > 0 and randomChance < Config.ProbabilityWeaponLoot then
											local randomAmmo = math.random(1, 30)
											GiveWeaponToPed(PlayerPedId(), randomWeapon, randomAmmo, true, false)
											ESX.ShowNotification('You found ' .. randomWeapon)
										elseif randomChance >= Config.ProbabilityWeaponLoot and randomChance < Config.ProbabilityMoneyLoot then
											TriggerServerEvent('esx_zombiesystem:moneyloot')
											local randomx = math.random(50, 300)
											exports.XNLRankBar:Exp_XNL_AddPlayerXP(randomx)
											TriggerServerEvent('AddXp', randomx)	
										elseif randomChance >= Config.ProbabilityMoneyLoot and randomChance < Config.ProbabilityItemLoot then
											TriggerServerEvent('esx_zombiesystem:itemloot', randomItem)
											local randomx = math.random(50, 300)
											exports.XNLRankBar:Exp_XNL_AddPlayerXP(randomx)
											TriggerServerEvent('AddXp', randomx)
										elseif randomChance >= Config.ProbabilityItemLoot and randomChance < 100 then
											local randomz = math.random(20, 100)
											exports.XNLRankBar:Exp_XNL_AddPlayerXP(randomz)
											TriggerServerEvent('AddXp', randomz)
											ESX.ShowNotification('You found nothing')
										end

I added these 3 lines so whenever the player kills a zombie and loots him , he will get a random amout of xp

local randomx = math.random(50, 300)
											exports.XNLRankBar:Exp_XNL_AddPlayerXP(randomx)
											TriggerServerEvent('AddXp', randomx)	

the local random stuff is just to make the random xp amout however the 2 other lines are part of xnlrankbar so in order for the to work you need xnlrankbar .

this line is for visual ( so you can see the xp adding in the bar )
exports.XNLRankBar:Exp_XNL_AddPlayerXP(randomx)

this line is to save the same amount that added to the xp bar to your database
TriggerServerEvent(‘AddXp’, randomx) and you need to add a small server part in the server.lua in esx_zombiesystem so the triggerserverevent works .

this is the server side that i added :slight_smile:

 ESX  = nil

TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

players = {}


RegisterServerEvent('AddXp')
AddEventHandler('AddXp',function(value)
	
	local _source = source
	local xPlayer  = ESX.GetPlayerFromId(_source)
	MySQL.Async.fetchAll('SELECT `xp` FROM users WHERE identifier = @identifier', {
		['@identifier'] = xPlayer.identifier
	}, function(result)
        -- print(result[1].rank)
       local xpbd = result[1].xp
   	
	MySQL.Async.fetchAll("UPDATE users SET xp = @xp WHERE identifier = @identifier",
                {
                 ['@identifier'] = xPlayer.identifier,
				 ['@xp'] = xpbd + (value)
                }
                )
	 end)
end)

also you need to add this to _resource.lua so the server part works

server_script "@mysql-async/lib/MySQL.lua"

um and also i forgot to mention that the player won’t gain any xp until he loots the zombie .

Ok man,
thanks.
this is very helpful,
I will try it

1 Like

well thanks to @VenomXNL , @sixsens and @Jules_Lucas it was very easy to set all of this up
thanks for @sixsens for 70% of the server file and @Jules_Lucas for 30% of the server file + some basic knowledge for ESX and the instructions that @VenomXNL mentioned .
so this is my XNLRANKBAR for ESX fully working and saving levels (xp) to database so player levels are saved .
XNLRankBar.rar (13.2 KB)
Steps to make this work :
-download the file and extract to your ressources folder .
-add start XNLRankBar in your config file .
-go to users table in your database -> then columns -> add a column called ‘xp’ with this options .


-now save and start your server .
-now the rank bar will showup if you use NUMPAD8 ( you can change it in client.lua line 110 )
-i set max rank to 5000 but you can change it in client.lua line 154
-> the rank bar is fully working and saving .
-> it’s up to you to use the four lines below in your scripts :

to gain xp :

exports.XNLRankBar:Exp_XNL_AddPlayerXP(100)
TriggerServerEvent(‘AddXp’, 100)

to remove xp :

exports.XNLRankBar:Exp_XNL_RemovePlayerXP(100)
TriggerServerEvent(‘RemoveXp’, 100)

->you can change the value ‘100’ to any other value you want . [note: 100 is the xp]

PS : DON’T FORGET THAT THE RANK BAR WON’T ADD XP ON ITS OWN ! YOU SHOULD USE THE LINES ABOVE TO ADD/REMOVE XP FOR A CERTAIN ACTION IN YOUR SCRIPTS !
NOTE : i forgot to mention that i changed the rankbar color so you can change it back in line 694 .

COLOR NUMBERS :slight_smile:

            PURE_WHITE = 0
	WHITE 		= 1
	BLACK 		= 2
	GREY 		= 3
	GREYLIGHT 	= 4
	GREYDARK 	= 5
	RED 		= 6
	REDLIGHT 	= 7
	REDDARK 	= 8 <- the one i'm  actually using
	BLUE 		= 9
	BLUELIGHT 	= 10
	BLUEDARK 	= 11
	YELLOW 		= 12
	YELLOWLIGHT = 13
	YELLOWDARK 	= 14
	ORANGE 		= 15
	ORANGELIGHT = 16
	ORANGEDARK 	= 17
	GREEN 		= 18
	GREENLIGHT 	= 19
	GREENDARK 	= 20
	PURPLE 		= 21
	PURPLELIGHT	= 22
	PURPLEDARK 	= 23
	PINK 		= 24
	BRONZE 		= 107
	SILVER 		= 108
	GOLD 		= 109
	PLATINUM 	= 110
	FREEMODE 	= 116	-- This is the 'normal blue color' for the Rankbar

this how to setup xnlrankbar + if you know how to add infection let me know please

i don’t really know about esx_xp never used it however i use a similar ressource called xnlrankbar ( i think it’s better but it’s hard to setup , well the hud of both scripts look the same since they both use gta o style however the one i use doesn’t have a leaderboard ) .
my esx_zombiesystem \client\main.lua line 526 looks like this

its work, thanks.

But what if zombies die and immediately give XP to the player?

xp for the kill i don’t really know , i couldn’t find any help or solution so i used loot as a trigger to xp gain xD
but i have this code but works with players not peds and i couldn’t find a way to convert it to peds so if you happen to solve it and make it work let me know .

-- SERVER

AddEventHandler("baseevents:onPlayerKilled", function(killer, reason)

  -- Give Player Money Server Sided (database options available later)

  TriggerClientEvent("PlayerKilled:Notification", killer)

end)

-- CLIENT

RegisterNetEvent("PlayerKilled:Notification")

AddEventHandler("PlayerKilled:Notification", function()

  SetTextComponentFormat("STRING")

  AddTextComponentString("You killed a player")

  DisplayHelpTextFromStringLabel(0, 0, 1, -1)

end)

never tried it so i don’tknow if it works .

1 Like

ok fine, I’m interested in seeing the “extinction” server where when a zombie is killed by a player it will immediately give XP to the player.
Now, I’ll try first, if I don’t find a solution, I’ll make it your way.

Thank you very much for helping

1 Like

ok if you have a infection script please let me know !

i dont have a infection script,
I also haven’t thought about including the infection feature on the server.

Hi
Do you know how to get xp killing players?

local xp = math.random(5, 25)

local price = math.random(1000, 3000)

AddEventHandler(“baseevents:onPlayerKilled”, function(killer, reason)

local xPlayer = ESX.GetPlayerFromId(source)

xPlayer.addAccountMoney(‘bank’, price)

TriggerEvent(‘esx_xp:addXP’, xPlayer.source , xp)

end)

:sunglasses::sunglasses:

Where do I put that? I have tried it and nothing

Any server Side

1 Like