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

Why does the level value become 0?

Hi! I am making a level system for my game. I have been watching some videos and tried to do some stuff alone. Why is the Level value resetting to 0 instead of increasing? Script:

while true do
	wait()
	for i,v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild("leaderstats") and v then
			if v.leaderstats.Level.Value == 0 and v.Data.Xp.Value >= 50 then
			v.leaderstats.Level.Value = v.leaderstats.Level.Value == 1
			v.Data.Xp.Value = 0
			end
			end
		if v:FindFirstChild("leaderstats") and v then
			if v.leaderstats.Level.Value == 1 and v.Data.Xp.Value >= 100 then
			v.leaderstats.Level.Value = v.leaderstats.Level.Value == 2
			v.Data.Xp.Value = 0
			end
			end
		if v:FindFirstChild("leaderstats") and v then
			if v.leaderstats.Level.Value == 2 and v.Data.Xp.Value >= 150 then
			v.leaderstats.Level.Value = v.leaderstats.Level.Value == 3
			v.Data.Xp.Value = 0
			end
			end
		if v:FindFirstChild("leaderstats") and v then
			if v.leaderstats.Level.Value == 3 and v.Data.Xp.Value >= 200 then
			v.leaderstats.Level.Value = v.leaderstats.Level.Value == 4
			v.Data.Xp.Value = 0
			end
			end
			
	end
	end

1 answer

Try this. It should be more efficient as well.

while true do
	wait()
	for i,v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild("leaderstats") and v then
			local amount = (v.leaderstats.Level.Value*50)+50 -- This will still maintain the needed value for levelling up, and will work for any number.
			if v.Data.Xp.Value >= amount then
				v.leaderstats.Level.Value = v.leaderstats.Level.Value + 1
				v.Data.Xp.Value = v.Data.Xp.Value - amount -- If the Xp exceeds the required number, we want to keep the leftovers.
			end
		end
	end
end

That will see if the Exp is 50 if the level is 0, 100 if the level is 1 and so on, and it will simply increase the level by 1, therefore, eliminating the need of so many lines.

The reason it set it to 0 is probably because you put:

v.leaderstats.Level.Value == 1

that will return a boolean. In this case, false. It makes the value "false" which makes no sense, because I'm assuming you're using an IntValue, not a BoolValue. Setting an IntValue to a boolean makes it 0. The correct method is:

v.leaderstats.Level.Value + 1 which will increase it by 1 no matter what number it is.

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