Jump to content

[HELP] Level requirements


VenomOG

Recommended Posts

hey ,

so firstly thank you for looking at my problem
and here is my problem so i have a level system
and i want to make diffrent xp needed to level up example 
LEVELS = {
[1] = 3000
[2] = 6000
},
see 1 need 3000 xp and 2 need 6000 xp 

 

Quote

how can i make it like this i use data
for levels example
setElementData(source,"Level",etc)
setElementData(source,"kill.level",etc) - xp

 

Link to comment

Test it please. I did not check this code, I wrote it directly in the editor without testing. I tried to find on the Internet already ready for 2014-2015, but I could not find it.

-- enable OOP in meta.xml 
	-- <oop>true</oop>

-- Author: XaskeL. 13 mar., 2019

local levels = {
	[1] = 1000, -- EXP needed to get this level
}

local function getPlayerLevel()
	return localPlayer:getData('level') or 0
end

local function getPlayerExp()
	return localPlayer:getData('exp') or 0
end

local function getPlayerNext()
	local level = getPlayerLevel()
	local current = getPlayerExp()
	local next = levels[level + 1]
	return next-current
end

local function givePlayerExp(i)
	assert(type(i) == "number", "invalid argument type in function givePlayerExp. Was expected integer.")
	local level = getPlayerLevel()
	local current = getPlayerExp()
	local next = levels[level + 1] - current
	if current+i >= next then
		next = (current+i)-next
		localPlayer:setData('level', level + 1)
		localPlayer:setData('exp', next)
	else
		localPlayer:setData('exp', current + i)
	end
	return true
end


 

Edited by XaskeL
Link to comment

@Knuck you can simply iterate through table

function addXp(player, amount)
   local xp = getElementData(player, "XP") + amount
   -- move to new level
   for i, v in pairs (LEVELS) do
      if xp >= v then
         xp = v - xp -- if you need it ofc
         setElementData(player, "Level", xp)
         break
      end
   end
   setElementData(player, "XP", xp)
end

 

Edited by JeViCo
  • Thanks 1
Link to comment
1 hour ago, JeViCo said:

@Knuck you can simply iterate through table


function addXp(player, amount)
   local xp = getElementData(player, "XP") + amount
   -- move to new level
   for i, v in pairs (LEVELS) do
      if xp >= v then
         xp = v - xp -- if you need it ofc
         setElementData(player, "Level", xp)
         break
      end
   end
   setElementData(player, "XP", xp)
end

 

It goes to the needed xp the just breaks even when I added the rest levels

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