Jump to content

need help for code based time event


Recommended Posts

i need a code for play a music when character levels up

so here is the problem every monster has different experience points and i can't figure out how to do

i need help for code something like

character has tag on every level

like

if inst.level >0 and inst.level <=10 then
 inst:HasTag("level1")
  .
  .
  .
  elseif inst.level >10 and inst.level <= 21 then
  inst:RemoveTag("level1")
  inst:AddTag("level2")
  .
  .
  .
  .
  end

and i need code for if player in few seconds levels up (i don't know how to make some code based time) the music plays

The reason i need this code is when i use my code it plays everytime character gain exp but i must be only when it's level up

Link to comment
Share on other sites

You should start by renaming your "level" variable to "experience". Then wherever you add experience to them, you do one of two things, depending on how you want this experience thing to work.

Version 1: Go to zero experience each level

Version 2: Continuously increasing experience

For both versions, you'll need to define how much experience is needed for each level. Is it just 10 experience per level, or does the experience needed increase with each level? For my examples below, I'll assume you just want it to be 10 experience per level, given your example code-snippet.

Version 1:

-- I assume the player (inst) has an amount of experience and a number representing their level and that their level starts at 1.
local function getExperienceNeededForLevel(level)
    -- You seem to have it at 10 exp per level regardless how many levels the player has, so we just always return 10.
    -- If you want to make it so the player needs more exp for each level, e.g. 10, 20, 30, you can make an algorithm for that.
    -- You can search for those on Google.
	return 10
end

local function receiveExp(inst, exp)
	inst.experience = inst.experience + exp
	var expNeededForLevel = getExperienceNeededForLevel(inst.level)
	while inst.experience >= expNeededForLevel do
			inst.experience = inst.experience - expNeededForLevel
			inst.level = inst.level + 1
			expNeededForLevel = getExperienceNeededForLevel(inst.level)
	end
end

 

Version 2:

-- I assume the player (inst) has an amount of experience and a number representing their level and that their level starts at 1.
local function getExperienceNeededForLevel(level)
	-- You seem to have it at 10 exp per level regardless how many levels the player has, but now we do not go back to 0 on exp,
	-- so we you want to make it so the player needs more exp for each level, e.g. 10, 20, 30: return level * 10
    -- If you want to make it so the player needs more exp for each level, e.g. 10, 20, 30, you can make an algorithm for that.
    -- You can search for those on Google.
	return level * 10
end

local function receiveExp(inst, exp)
	inst.experience = inst.experience + exp
	var expNeededForLevel = getExperienceNeededForLevel(inst.level)
	while inst.experience >= expNeededForLevel do
			inst.level = inst.level + 1
			expNeededForLevel = getExperienceNeededForLevel(inst.level)
	end
end

 

Edited by Ultroman
Link to comment
Share on other sites

13 hours ago, Ultroman said:

You should start by renaming your "level" variable to "experience". Then wherever you add experience to them, you do one of two things, depending on how you want this experience thing to work.

Version 1: Go to zero experience each level

Version 2: Continuously increasing experience

For both versions, you'll need to define how much experience is needed for each level. Is it just 10 experience per level, or does the experience needed increase with each level? For my examples below, I'll assume you just want it to be 10 experience per level, given your example code-snippet.

Version 1:


-- I assume the player (inst) has an amount of experience and a number representing their level and that their level starts at 1.
local function getExperienceNeededForLevel(level)
	-- You seem to have it at 10 exp per level regardless how many levels the player has, so we just always return 10.
	-- If you want to make it so the player needs more exp for each level, e.g. 10, 20, 30, you can do: return level * 10
	return 10
end

local function receiveExp(inst, exp)
	inst.experience = inst.experience + exp
	var expNeededForLevel = getExperienceNeededForLevel(inst.level)
	while inst.experience >= expNeededForLevel do
			inst.experience = inst.experience - expNeededForLevel
			inst.level = inst.level + 1
			expNeededForLevel = getExperienceNeededForLevel(inst.level)
	end
end

 

Version 2:


-- I assume the player (inst) has an amount of experience and a number representing their level and that their level starts at 1.
local function getExperienceNeededForLevel(level)
	-- You seem to have it at 10 exp per level regardless how many levels the player has, but now we do not go back to 0 on exp,
	-- so we you want to make it so the player needs more exp for each level, e.g. 10, 20, 30: return level * 10
	return level * 10
end

local function receiveExp(inst, exp)
	inst.experience = inst.experience + exp
	var expNeededForLevel = getExperienceNeededForLevel(inst.level)
	while inst.experience >= expNeededForLevel do
			inst.level = inst.level + 1
			expNeededForLevel = getExperienceNeededForLevel(inst.level)
	end
end

 

I don't have any knowledge about coding and english is not my first language so it's hard for me to make some codes. Thanks a lot for helping.

 

But i have one question

what happens if the gained exp is much more than needed like

if character needs 30 exp for level up but the mob gives 50 exp? does it takes 30 exp and reset or takes 30 and hold 20?

or something like this

character needs 20exp for level up and for next level it needs 30 exp and mob gives 50 exp does it level up 2 times?

Edited by AkaiNight
Link to comment
Share on other sites

That's what the while-loop is there for. The function  adds the experience, and then tries to increase the level (and in version 1 subtract the experience needed for the current level) until experience is no longer above the experience needed for the next level. If you have no idea how to code, then I first have to ask you if you want to or if you just want this to work and then never touch coding again.

Link to comment
Share on other sites

21 minutes ago, Ultroman said:

That's what the while-loop is there for. The function  adds the experience, and then tries to increase the level (and in version 1 subtract the experience needed for the current level) until experience is no longer above the experience needed for the next level. If you have no idea how to code, then I first have to ask you if you want to or if you just want this to work and then never touch coding again.

well i don't want to make people do my work but im not fullly able to do it. So i have part one of this code 

local function levelsystem(inst, data)

local health_percent = inst.components.health:GetPercent()
local sanity_percent = inst.components.sanity:GetPercent()
local hunger_percent = inst.components.hunger:GetPercent()

if inst.level = 0 then

	inst.components.health.maxhealth = math.ceil (50)
	inst.components.hunger.max = math.ceil (50)
	inst.components.sanity.max = math.ceil (50)
elseif inst.level = 1 then
	inst.components.health.maxhealth = math.ceil (55)
	inst.components.hunger.max = math.ceil (55)
	inst.components.sanity.max = math.ceil (55)
elseif inst.level = 2 then
	inst.components.health.maxhealth = math.ceil (60)
	inst.components.hunger.max = math.ceil (60)
	inst.components.sanity.max = math.ceil (60)
elseif inst.level = 3 then
	inst.components.health.maxhealth = math.ceil (65)
	inst.components.hunger.max = math.ceil (65)
	inst.components.sanity.max = math.ceil (65)
elseif inst.level = 4 then
	inst.components.health.maxhealth = math.ceil (70)
	inst.components.hunger.max = math.ceil (70)
	inst.components.sanity.max = math.ceil (70)
elseif inst.level = 5 then
	inst.components.health.maxhealth = math.ceil (75)
	inst.components.hunger.max = math.ceil (75)
	inst.components.sanity.max = math.ceil (75)
elseif inst.level = 6 then
	inst.components.health.maxhealth = math.ceil (80)
	inst.components.hunger.max = math.ceil (80)
	inst.components.sanity.max = math.ceil (80)
elseif inst.level = 7 then
	inst.components.health.maxhealth = math.ceil (85)
	inst.components.hunger.max = math.ceil (85)
	inst.components.sanity.max = math.ceil (85)
elseif inst.level = 8 then
	inst.components.health.maxhealth = math.ceil (90)
	inst.components.hunger.max = math.ceil (90)
	inst.components.sanity.max = math.ceil (90)
elseif inst.level = 9 then
	inst.components.health.maxhealth = math.ceil (95)
	inst.components.hunger.max = math.ceil (95)
	inst.components.sanity.max = math.ceil (95)
elseif inst.level = 10 then
	inst.components.health.maxhealth = math.ceil (100)
	inst.components.hunger.max = math.ceil (100)
	inst.components.sanity.max = math.ceil (100)
elseif inst.level = 11 then
	inst.components.health.maxhealth = math.ceil (110)
	inst.components.hunger.max = math.ceil (110)
	inst.components.sanity.max = math.ceil (110)
elseif inst.level = 12 then
	inst.components.health.maxhealth = math.ceil (120)
	inst.components.hunger.max = math.ceil (120)
	inst.components.sanity.max = math.ceil (120)
elseif inst.level = 13 then
	inst.components.health.maxhealth = math.ceil (130)
	inst.components.hunger.max = math.ceil (130)
	inst.components.sanity.max = math.ceil (130)
elseif inst.level = 14 then
	inst.components.health.maxhealth = math.ceil (140)
	inst.components.hunger.max = math.ceil (140)
	inst.components.sanity.max = math.ceil (140)
elseif inst.level = 15 then
	inst.components.health.maxhealth = math.ceil (150)
	inst.components.hunger.max = math.ceil (150)
	inst.components.sanity.max = math.ceil (150)
elseif inst.level = 16 then
	inst.components.health.maxhealth = math.ceil (160)
	inst.components.hunger.max = math.ceil (160)
	inst.components.sanity.max = math.ceil (160)
elseif inst.level = 17 then
	inst.components.health.maxhealth = math.ceil (170)
	inst.components.hunger.max = math.ceil (170)
	inst.components.sanity.max = math.ceil (170)
elseif inst.level = 18 then
	inst.components.health.maxhealth = math.ceil (180)
	inst.components.hunger.max = math.ceil (180)
	inst.components.sanity.max = math.ceil (180)
elseif inst.level = 19 then
	inst.components.health.maxhealth = math.ceil (190)
	inst.components.hunger.max = math.ceil (190)
	inst.components.sanity.max = math.ceil (190)
elseif inst.level = 20 then
	inst.components.health.maxhealth = math.ceil (200)
	inst.components.hunger.max = math.ceil (200)
	inst.components.sanity.max = math.ceil (200)
elseif inst.level = 21 then
	inst.components.health.maxhealth = math.ceil (210)
	inst.components.hunger.max = math.ceil (210)
	inst.components.sanity.max = math.ceil (210)
elseif inst.level = 22 then
	inst.components.health.maxhealth = math.ceil (220)
	inst.components.hunger.max = math.ceil (220)
	inst.components.sanity.max = math.ceil (220)
elseif inst.level = 23 then
	inst.components.health.maxhealth = math.ceil (230)
	inst.components.hunger.max = math.ceil (230)
	inst.components.sanity.max = math.ceil (230)
elseif inst.level = 24 then
	inst.components.health.maxhealth = math.ceil (240)
	inst.components.hunger.max = math.ceil (240)
	inst.components.sanity.max = math.ceil (240)
elseif inst.level = 25 then
	inst.components.health.maxhealth = math.ceil (250)
	inst.components.hunger.max = math.ceil (250)
	inst.components.sanity.max = math.ceil (250)
elseif inst.level = 26 then
	inst.components.health.maxhealth = math.ceil (260)
	inst.components.hunger.max = math.ceil (260)
	inst.components.sanity.max = math.ceil (260)
elseif inst.level = 27 then
	inst.components.health.maxhealth = math.ceil (270)
	inst.components.hunger.max = math.ceil (270)
	inst.components.sanity.max = math.ceil (270)
elseif inst.level = 28 then
	inst.components.health.maxhealth = math.ceil (280)
	inst.components.hunger.max = math.ceil (280)
	inst.components.sanity.max = math.ceil (280)
elseif inst.level = 29 then
	inst.components.health.maxhealth = math.ceil (290)
	inst.components.hunger.max = math.ceil (290)
	inst.components.sanity.max = math.ceil (290)
elseif inst.level = 30 then
	inst.components.health.maxhealth = math.ceil (300)
	inst.components.hunger.max = math.ceil (300)
	inst.components.sanity.max = math.ceil (300)
end

	inst.components.health:SetPercent(health_percent)
	inst.components.hunger:SetPercent(hunger_percent)
	inst.components.sanity:SetPercent(sanity_percent)
end

and im working on how to gain experience i just couldn't figure this part

local function receiveExp(inst, exp)
	inst.experience = inst.experience + exp
	var expNeededForLevel = getExperienceNeededForLevel(inst.level)
	while inst.experience >= expNeededForLevel do
			inst.level = inst.level + 1
			expNeededForLevel = getExperienceNeededForLevel(inst.level)
	end
end

i did understand what this is for but i don't have any idea about "exp" part i was using this

local function onkill(inst, data)

	local victim = data.victim
	
	if (
	    victim:HasTag("catcoon")
	) then

	inst.level = inst.level -969696
	inst.components.talker:Say("There is nothing to say... not even 'Nya~'")
end
	
	if (
		victim:HasTag("chester") or
		victim:HasTag("glommer") or
		victim:HasTag("hutch") or
		victim:HasTag("lavae_pet") 
	) then
	
	inst.level = inst.level - 50
	inst.components.talker:Say("Oh come on! Why i did that? I'm cursed now. Nya~")
end

it contunies with all mobs and im not sure how to change this with exp system. Is it ok if i just change inst.level =inst.level -50 or should i do something with exp thing or should i just delete inst.experience = inst.experience + exp and make something with the code i use before 

Edited by AkaiNight
Link to comment
Share on other sites

and here is an other question what happens if i just do that

local function getExperienceNeededForLevel(level)
	return level * 10
end

local function receiveExp(inst, data)
  	local max_exp = 999999999994650
	local min_exp = -99999999999999
  
	var expNeededForLevel = getExperienceNeededForLevel(inst.level)
	while inst.experience >= expNeededForLevel do
			inst.level = inst.level + 1
			expNeededForLevel = getExperienceNeededForLevel(inst.level)
	end
end

local function onkill (inst, data)
local victim = data.victim
	
	if (
		victim:HasTag("chester") or
		victim:HasTag("glommer") or
		victim:HasTag("hutch") or
		victim:HasTag("lavae_pet") 
	) then

inst.experience = inst.experience -50
recieveExp(inst)
end

something like that im not sure...

Link to comment
Share on other sites

nope i just failed. Its over me... im not giving up here is the last version(demo) of my code but i don't have time for try :(

 

local function onkill (inst, data)
local victim = data.victim
	if (
	    victim:HasTag("spider")
	) then

inst.experience = inst.experience + 1
end
end
local function levelexp(inst, data)

	local max_exp = 999999999994650
	local min_exp = -99999999999999
	local level = math.min(inst.level, max_exp)
	
	local health_percent = inst.components.health:GetPercent()
	local sanity_percent = inst.components.sanity:GetPercent()
	local hunger_percent = inst.components.hunger:GetPercent()
	
	var expNeededForLevel = getExperienceNeededForLevel(inst.data)
	while inst.experience >= expNeededForLevel do
		inst.level = inst.level + 1
		expNeededForLevel = getExperienceNeededForLevel(inst.level)
	end

if inst.level = 1 then 
inst.components.health.maxhealth = math.ceil (50)
inst.components.hunger.max = math.ceil (50)
inst.components.sanity.max = math.ceil (50)
end

	inst.components.health:SetPercent(health_percent)
	inst.components.hunger:SetPercent(hunger_percent)
	inst.components.sanity:SetPercent(sanity_percent)
end

local function getExperienceNeededForLevel(level)
	return level * 10
end

 

Edited by AkaiNight
Link to comment
Share on other sites

On 23.12.2019 at 4:17 PM, Ultroman said:

I don't have time right now, but at a glance I can tell you that you must always declare a function before you can use it. In the case of your last code snippet, you have to move getExperienceNeededForLevel above the rest of the code.

what do you mean with snipped

Link to comment
Share on other sites

A block of code specifically chosen for presentation is usually called "a code snippet" or "a snippet of code". It just means "a part of some larger code base", where a "code base" denotes, e.g., all the code for a mod or all the code for a game or all the code for the Steam API.

Link to comment
Share on other sites

2 hours ago, Ultroman said:

A block of code specifically chosen for presentation is usually called "a code snippet" or "a snippet of code". It just means "a part of some larger code base", where a "code base" denotes, e.g., all the code for a mod or all the code for a game or all the code for the Steam API.

okey i couldn't make this code work but im trying something like this

local function levelexp(inst, data)

local max_exp = 999999999994650
local min_exp = -99999999999999
local experience = math.min (inst.experience, max_exp, min_exp)
local max_level = 30
local min_level = 0
local level = math.min (inst.level, max_level, min_level)

if inst.level = 0 then
	inst.expneededforlevel= 10
elseif inst.level = 1 then
	inst.expneededforlevel= 20
end
while inst.experience >= inst.expneededforlevel do
	inst.level = inst.level + 1
end
end

but it stil crashes and i don't know what to do with this error it says "then" expected near "="

if inst.level = 0 then

here is line 46

Link to comment
Share on other sites

On 27.12.2019 at 9:44 AM, Ultroman said:

if inst.level = 0 then

should be


if inst.level == 0 then

 

ah i just thought it was just bugged :/ thanks

FINALLY I MADE IT! Thank you for all your help and advices. If anyone would need it im leaving it here 

local function levelexp(inst, data)

local max_exp = 999999999994650
local min_exp = -99999999999999
local experience = math.min (inst.experience, max_exp, min_exp)
local max_level = 30
local min_level = 0
local level = math.min (inst.level, max_level, min_level)

if inst.level == 0 then
	inst.expneededforlevel= 10
elseif inst.level == 1 then
	inst.expneededforlevel= 20
end

if inst.experience >= inst.expneededforlevel then
	inst.level = inst.level + 1
	inst.components.talker:Say("it works")
end

end

 

Link to comment
Share on other sites

9 hours ago, Ultroman said:

For future reference, what that code does, is start the level at 0, and then requires 10 experience for level 1. Then, from level 1 to 2 and onwards it requires 20 experience per level.

it was just a part pf code here is ful one

local function levelexp(inst, data)

local max_exp = 999999999994650
local min_exp = -99999999999999
local experience = math.min (inst.experience, max_exp, min_exp)
local max_level = 30
local min_level = 0
local level = math.min (inst.level, max_level, min_level)

local health_percent = inst.components.health:GetPercent()
local sanity_percent = inst.components.sanity:GetPercent()
local hunger_percent = inst.components.hunger:GetPercent()

if inst.level == 0 then
	inst.expneededforlevel= 10
elseif inst.level == 1 then
	inst.expneededforlevel= 20
	inst.components.health.maxhealth = math.ceil (55)
	inst.components.hunger.max = math.ceil (55)
	inst.components.sanity.max = math.ceil (55)
	
elseif inst.level == 2 then
	inst.expneededforlevel= 40
	inst.components.health.maxhealth = math.ceil (60)
	inst.components.hunger.max = math.ceil (60)
	inst.components.sanity.max = math.ceil (60)
	
elseif inst.level == 3 then
	inst.expneededforlevel= 70
	inst.components.health.maxhealth = math.ceil (65)
	inst.components.hunger.max = math.ceil (65)
	inst.components.sanity.max = math.ceil (65)
	
elseif inst.level == 4 then
	inst.expneededforlevel= 110
	inst.components.health.maxhealth = math.ceil (70)
	inst.components.hunger.max = math.ceil (70)
	inst.components.sanity.max = math.ceil (70)
	
elseif inst.level == 5 then
	inst.expneededforlevel= 160
	inst.components.health.maxhealth = math.ceil (75)
	inst.components.hunger.max = math.ceil (75)
	inst.components.sanity.max = math.ceil (75)
	
elseif inst.level == 6 then
	inst.expneededforlevel= 220
	inst.components.health.maxhealth = math.ceil (80)
	inst.components.hunger.max = math.ceil (80)
	inst.components.sanity.max = math.ceil (80)
	
elseif inst.level == 7 then
	inst.expneededforlevel= 290
	inst.components.health.maxhealth = math.ceil (85)
	inst.components.hunger.max = math.ceil (85)
	inst.components.sanity.max = math.ceil (85)
	
elseif inst.level == 8 then
	inst.expneededforlevel= 370
	inst.components.health.maxhealth = math.ceil (90)
	inst.components.hunger.max = math.ceil (90)
	inst.components.sanity.max = math.ceil (90)
	
elseif inst.level == 9 then
	inst.expneededforlevel= 460
	inst.components.health.maxhealth = math.ceil (95)
	inst.components.hunger.max = math.ceil (95)
	inst.components.sanity.max = math.ceil (95)
	
elseif inst.level == 10 then
	inst.expneededforlevel= 560
	inst.components.health.maxhealth = math.ceil (100)
	inst.components.hunger.max = math.ceil (100)
	inst.components.sanity.max = math.ceil (100)
	
elseif inst.level == 11 then
	inst.expneededforlevel= 670
	inst.components.health.maxhealth = math.ceil (110)
	inst.components.hunger.max = math.ceil (110)
	inst.components.sanity.max = math.ceil (110)
	
elseif inst.level == 12 then
	inst.expneededforlevel= 790
	inst.components.health.maxhealth = math.ceil (120)
	inst.components.hunger.max = math.ceil (120)
	inst.components.sanity.max = math.ceil (120)
	
elseif inst.level == 13 then
	inst.expneededforlevel= 920
	inst.components.health.maxhealth = math.ceil (130)
	inst.components.hunger.max = math.ceil (130)
	inst.components.sanity.max = math.ceil (130)
	
elseif inst.level == 14 then
	inst.expneededforlevel= 1060
	inst.components.health.maxhealth = math.ceil (140)
	inst.components.hunger.max = math.ceil (140)
	inst.components.sanity.max = math.ceil (140)
	
elseif inst.level == 15 then
	inst.expneededforlevel= 1210
	inst.components.health.maxhealth = math.ceil (150)
	inst.components.hunger.max = math.ceil (150)
	inst.components.sanity.max = math.ceil (150)
	
elseif inst.level == 16 then
	inst.expneededforlevel= 1370
	inst.components.health.maxhealth = math.ceil (160)
	inst.components.hunger.max = math.ceil (160)
	inst.components.sanity.max = math.ceil (160)
	
elseif inst.level == 17 then
	inst.expneededforlevel= 1540
	inst.components.health.maxhealth = math.ceil (170)
	inst.components.hunger.max = math.ceil (170)
	inst.components.sanity.max = math.ceil (170)
	
elseif inst.level == 18 then
	inst.expneededforlevel= 1720
	inst.components.health.maxhealth = math.ceil (180)
	inst.components.hunger.max = math.ceil (180)
	inst.components.sanity.max = math.ceil (180)
	
elseif inst.level == 19 then
	inst.expneededforlevel= 1910
	inst.components.health.maxhealth = math.ceil (190)
	inst.components.hunger.max = math.ceil (190)
	inst.components.sanity.max = math.ceil (190)
	
elseif inst.level == 20 then
	inst.expneededforlevel= 2110
	inst.components.health.maxhealth = math.ceil (200)
	inst.components.hunger.max = math.ceil (200)
	inst.components.sanity.max = math.ceil (200)
	
elseif inst.level == 21 then
	inst.expneededforlevel= 2320
	inst.components.health.maxhealth = math.ceil (210)
	inst.components.hunger.max = math.ceil (210)
	inst.components.sanity.max = math.ceil (210)
	
elseif inst.level == 22 then
	inst.expneededforlevel= 2540
	inst.components.health.maxhealth = math.ceil (220)
	inst.components.hunger.max = math.ceil (220)
	inst.components.sanity.max = math.ceil (220)
	
elseif inst.level == 23 then
	inst.expneededforlevel= 2770
	inst.components.health.maxhealth = math.ceil (230)
	inst.components.hunger.max = math.ceil (230)
	inst.components.sanity.max = math.ceil (230)
	
elseif inst.level == 24 then
	inst.expneededforlevel= 3010
	inst.components.health.maxhealth = math.ceil (240)
	inst.components.hunger.max = math.ceil (240)
	inst.components.sanity.max = math.ceil (240)
	
elseif inst.level == 25 then
	inst.expneededforlevel= 3260
	inst.components.health.maxhealth = math.ceil (250)
	inst.components.hunger.max = math.ceil (250)
	inst.components.sanity.max = math.ceil (250)
	
elseif inst.level == 26 then
	inst.expneededforlevel= 3520
	inst.components.health.maxhealth = math.ceil (260)
	inst.components.hunger.max = math.ceil (260)
	inst.components.sanity.max = math.ceil (260)
	
elseif inst.level == 27 then
	inst.expneededforlevel= 3790
	inst.components.health.maxhealth = math.ceil (270)
	inst.components.hunger.max = math.ceil (270)
	inst.components.sanity.max = math.ceil (270)
	
elseif inst.level == 28 then
	inst.expneededforlevel= 4070
	inst.components.health.maxhealth = math.ceil (280)
	inst.components.hunger.max = math.ceil (280)
	inst.components.sanity.max = math.ceil (280)
	
elseif inst.level == 29 then
	inst.expneededforlevel= 4360
	inst.components.health.maxhealth = math.ceil (290)
	inst.components.hunger.max = math.ceil (290)
	inst.components.sanity.max = math.ceil (290)
	
elseif inst.level == 30 then
	inst.expneededforlevel= 4660
	inst.components.health.maxhealth = math.ceil (300)
	inst.components.hunger.max = math.ceil (300)
	inst.components.sanity.max = math.ceil (300)
end

if inst.experience >= inst.expneededforlevel then
	inst.SoundEmitter:PlaySound("kitsura/characters/kitsura/level up")
	inst.level = inst.level + 1
	inst.components.talker:Say("Level Up!! Level \n".. (inst.level) )
end
	inst.components.health:SetPercent(health_percent)
	inst.components.hunger:SetPercent(hunger_percent)
	inst.components.sanity:SetPercent(sanity_percent)
end

 

Link to comment
Share on other sites

This function should return the values you want. I deconstructed your code into an algorithm, so you can save a bunch of code, maybe learn something, and also let your players level up to level 5000 with a continuously increasing exp requirement.

It returns two values. The first is the experience needed, and the second is the max stat value (for your health, hunger and sanity).

local extraExpRequiredPerLevel = 5
local extraMaxStatUntilLevel10 = 5
local extraMaxStatAfterLevel10 = 10

local getLevelData = function(level)
	return
		10 + (level * ((level + 1) * extraExpRequiredPerLevel)),
		50 + (level <= 10 and (level * extraMaxStatUntilLevel10) or ((10 * extraMaxStatUntilLevel10) + ((level - 10) * extraMaxStatAfterLevel10)))
end

Usage:

local expNeededForNextLevel, maxStatValue = getLevelData(inst.level)
inst.expneededforlevel = expNeededForNextLevel
inst.components.health.maxhealth = maxStatValue
inst.components.hunger.max = maxStatValue
inst.components.sanity.max = maxStatValue

That replaces all of your if-statements.

Edited by Ultroman
Link to comment
Share on other sites

1 hour ago, Ultroman said:

This function should return the values you want. I deconstructed your code into an algorithm, so you can save a bunch of code, maybe learn something, and also let your players level up to level 5000 with a continuously increasing exp requirement.

It returns two values. The first is the experience needed, and the second is the max stat value (for your health, hunger and sanity).


local extraExpRequiredPerLevel = 5
local extraMaxStatUntilLevel10 = 5
local extraMaxStatAfterLevel10 = 10

local getLevelData = function(level)
	return
		10 + (level * ((level + 1) * extraExpPerLevel),
		50 + (level <= 10 and (level * extraMaxStatUntilLevel10) or ((10 * extraMaxStatUntilLevel10) + ((level - 10) * extraMaxStatAfterLevel10)))
end

Usage:


local expNeededForNextLevel, maxStatValue = getLevelData(inst.level)
inst.expneededforlevel = expNeededForNextLevel
inst.components.health.maxhealth = maxStatValue
inst.components.hunger.max = maxStatValue
inst.components.sanity.max = maxStatValue

That replaces all of your if-statements.

wow i don't understand anythin on that code :/ thanks a lot btw it's ok if i just replace it right? because i never see any code like this

Link to comment
Share on other sites

This is my rewrite of your code. It's not the best it can be, but it's shorter and more extendable than your original code.

-- These are all essentially constants. No need to have them inside the function where they get declared every time.
-- This, in turn, keeps them in memory all the time. It is a trade-off. In this case, negligible.
local max_level = 30
local min_level = 0
local max_exp = 999999999994650
local min_exp = 0 -- no reason to let your players get negative experience
local startingExpNeeded = 10
local extraExpRequiredPerLevel = 5
local startingMaxStat = 50
local extraMaxStatUntilLevel10 = 5
local extraMaxStatAfterLevel10 = 10

local getExpNeededForLevel = function(level)
	-- The + 1 is only needed because your base-level is 0. If it was 1, then you would not need that.
	return startingExpNeeded + (level * ((level + 1) * extraExpRequiredPerLevel))
end

local getMaxStatValueForLevel = function(level)
	-- Calculate the max stat value for the given level.
	-- It looks daunting, but it's mostly comments. Read it slowly, following the comments.
	-- It'll make sense.
	return startingMaxStat +
		(
		-- if below or at level 10, it's just: level * extraMaxStatUntilLevel10
		level <= 10 and (level * extraMaxStatUntilLevel10)
		or
		-- If higher than level 10, add all the maxStat bonuses for the first 10 levels,
		-- and then the bonuses for however many levels above 10 you are.
		((10 * extraMaxStatUntilLevel10) + ((level - 10) * extraMaxStatAfterLevel10))
		)
end

local function levelexp(inst, data)
	local level = math.min (inst.level, max_level, min_level)
	local experience = math.min (inst.experience, max_exp, min_exp)
	local expneededforlevel = getExpNeededForLevel(level)
	
	-- Exit early if the player is max level or does not have enough experience for next level.
	if level >= max_level or inst.experience < expneededforlevel then
		return
	end
	
	local health_percent = inst.components.health:GetPercent()
	local sanity_percent = inst.components.sanity:GetPercent()
	local hunger_percent = inst.components.hunger:GetPercent()
	
	-- Using a while-loop allows your players to gain several levels with 1 call to this function.
	while inst.experience >= expneededforlevel do
		level = level + 1
		expneededforlevel = getExpNeededForLevel(level)
	end
	
	inst.level = level
	
	local maxStatValueForLevel = getMaxStatValueForLevel(level)
	
	inst.components.health.maxhealth = maxStatValueForLevel
	inst.components.hunger.max = maxStatValueForLevel
	inst.components.sanity.max = maxStatValueForLevel
	
	inst.components.health:SetPercent(health_percent)
	inst.components.hunger:SetPercent(hunger_percent)
	inst.components.sanity:SetPercent(sanity_percent)
	
	-- Only play sound and say something once, even though several levels may have been gained.
	inst.SoundEmitter:PlaySound("kitsura/characters/kitsura/level up")
	inst.components.talker:Say("Level Up!! Level \n"..(inst.level))
end

 

Edited by Ultroman
Link to comment
Share on other sites

10 hours ago, Ultroman said:

This is my rewrite of your code. It's not the best it can be, but it's shorter and more extendable than your original code.


-- These are all essentially constants. No need to have them inside the function where they get declared every time.
-- This, in turn, keeps them in memory all the time. It is a trade-off. In this case, negligible.
local max_level = 30
local min_level = 0
local max_exp = 999999999994650
local min_exp = 0 -- no reason to let your players get negative experience
local startingExpNeeded = 10
local extraExpRequiredPerLevel = 5
local startingMaxStat = 50
local extraMaxStatUntilLevel10 = 5
local extraMaxStatAfterLevel10 = 10

local getExpNeededForLevel = function(level)
	-- The + 1 is only needed because your base-level is 0. If it was 1, then you would not need that.
	return startingExpNeeded + (level * ((level + 1) * extraExpRequiredPerLevel))
end

local getMaxStatValueForLevel = function(level)
	-- Calculate the max stat value for the given level.
	-- It looks daunting, but it's mostly comments. Read it slowly, following the comments.
	-- It'll make sense.
	return startingMaxStat +
		(
		-- if below or at level 10, it's just: level * extraMaxStatUntilLevel10
		level <= 10 and (level * extraMaxStatUntilLevel10)
		or
		-- If higher than level 10, add all the maxStat bonuses for the first 10 levels,
		-- and then the bonuses for however many levels above 10 you are.
		((10 * extraMaxStatUntilLevel10) + ((level - 10) * extraMaxStatAfterLevel10))
		)
end

local function levelexp(inst, data)
	local level = math.min (inst.level, max_level, min_level)
	local experience = math.min (inst.experience, max_exp, min_exp)
	local expneededforlevel = getExpNeededForLevel(level)
	
	-- Exit early if the player is max level or does not have enough experience for next level.
	if level >= max_level or inst.experience < expneededforlevel then
		return
	end
	
	local health_percent = inst.components.health:GetPercent()
	local sanity_percent = inst.components.sanity:GetPercent()
	local hunger_percent = inst.components.hunger:GetPercent()
	
	-- Using a while-loop allows your players to gain several levels with 1 call to this function.
	while inst.experience >= expneededforlevel do
		level = level + 1
		expneededforlevel = getExpNeededForLevel(level)
	end
	
	inst.level = level
	
	local maxStatValueForLevel = getMaxStatValueForLevel(level)
	
	inst.components.health.maxhealth = maxStatValueForLevel
	inst.components.hunger.max = maxStatValueForLevel
	inst.components.sanity.max = maxStatValueForLevel
	
	inst.components.health:SetPercent(health_percent)
	inst.components.hunger:SetPercent(hunger_percent)
	inst.components.sanity:SetPercent(sanity_percent)
	
	-- Only play sound and say something once, even though several levels may have been gained.
	inst.SoundEmitter:PlaySound("kitsura/characters/kitsura/level up")
	inst.components.talker:Say("Level Up!! Level \n"..(inst.level))
end

 

thanks a lot

Link to comment
Share on other sites

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
 Share

×
  • Create New...