Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make an if statement that works for all levels in a leveling system?

game.Players.PlayerAdded:connect(function(player)

local stats = Instance.new('IntValue', player)
stats.Name = "leaderstats"

local Kills = Instance.new("IntValue", stats)
Kills.Name = 'Kills'

local Deaths = Instance.new("IntValue", stats)
Deaths.Name = 'Deaths'

local EXP = Instance.new("IntValue", stats)
EXP.Name = "EXP"
EXP.Value = 0

local Level = Instance.new("IntValue", stats)
Level.Name = "Level"
Level.Value = 1

player.CharacterAdded:connect(function(Character)
	wait()
	Character.Humanoid.Died:connect(function()
		EXP.Value = EXP.Value + 100 --Each kill will give 100 EXP
		Deaths.Value = Deaths.Value+1
		wait()
		if Character.Humanoid:FindFirstChild('creator') then
			local Killer = Character.Humanoid.creator.Value
			Killer.leaderstats.Kills.Value = Killer.leaderstats.Kills.Value + 1
		end
	end)
end)

if Level then --This if statement wont work for all levels D:
	local anotherOne = Level + 1
	if EXP.Value == anotherOne * 1000 then
		EXP.Value = 0
		Level.Value = Level.Value + 1
	end
end

end)

--Having trouble making my if statement work for all levels. --I made EXP.Value = 0 so the EXP would reset and the player could start earning EXP again for the next level.

1 comment

0

Add a RequiredXP value

2 answers

✓ Accepted answer

Firstly, you need to compute how many experience points are required in order to advance to the next level. For example, if a player is at level 1, they'll need 1000 EXP to reach level 2, but if they're at level 2, they'll need 2000 EXP before they can reach level 3. With this knowledge, we can write a function that takes a level number as input, and returns the required EXP to reach the next level as output:

function expForLevel(level)
	return level * 1000
end

This function doesn't have to be so simple - you can experiment with different mappings from a level number to an experience requirement for a different kind of level progression

Then, in your conditional if statement, you need to check if the player's EXP is equal to or greater than the requirement to level-up:

requirement = expForLevel(Level.Value) --how much EXP the player needs
if EXP.Value >= requirement then --has the player gained enough EXP?
	--Yes! Level them up.
	Level.Value = Level.Value + 1
	EXP.Value = 0
end

Now, concerning how the EXP is reset, it might be better to carry over any surplus EXP from the previous level, instead of setting the EXP back to 0. For example, if a player needs 1000 EXP and already has 950 EXP, but suddenly earns 100 more points - giving them a total of 1050 - then with the current implementation that 50 EXP will be discarded. Instead of setting the EXP.Value to 0, you could subtract the amount of EXP required to level up, thus carrying over extra EXP points from one level to another!

requirement = expForLevel(Level.Value)
if EXP.Value >= requirement then
	Level.Value = Level.Value + 1
	EXP.Value = EXP.Value - requirement --subtract the amount needed to level-up
end

1 comment

0

THANK YOU!!! I forgot that I can use return and great suggestion on subtracting the required exp.

If filtering enabled is on (which is highly recommended to keep your game secure), be sure to use remote events when replicating the exp points and the levels to the server!

This discussion is preserved as it was. New answers, comments, and votes are closed.