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

Why doesn't this script work?

I know this script is very crude, but I haven't really learned how to use tables so this will have to do. Basically, I want a script that constantly checks a player's Experience to see if it's enough to level up. I have a script that sets my Experience to 100,000, but the script isn't leveling me up, I just stay at level 1, and nothing gets put in the output to let me know what's going wrong.



--Start of functions

while wait() do
Player = game.Players.LocalPlayer
if Player.Exp.Value >= 100 < 300 then
        Player.leaderstats.PlayerLevel.Value = 2
    elseif Player.Exp.Value >= 300 < 600 then
        Player.leaderstats.PlayerLeve.Valuel = 3
    elseif Player.Exp.Value >= 600 < 1000 then
        Player.leaderstats.PlayerLeve.Valuel = 4
    elseif Player.Exp.Value >= 1000 < 1500 then
        Player.leaderstats.PlayerLevel.Value = 5
    elseif Player.Exp.Value >= 1500 < 2000 then
        Player.leaderstats.PlayerLevel.Value = 6
    elseif Player.Exp.Value >= 2000 < 2500 then
        Player.leaderstats.PlayerLevel.Value = 7
    elseif Player.Exp.Value >= 2500 < 3000 then
        Player.leaderstats.PlayerLevel.Value = 8
    elseif Player.Exp.Value >= 3000 < 4000 then
        Player.leaderstats.PlayerLevel.Value = 9
    elseif Player.Exp.Value >= 4000 < 5000 then
        Player.leaderstats.PlayerLevel.Value = 10
    elseif Player.Exp.Value >= 5000 < 6500 then
        Player.leaderstats.PlayerLevel.Value = 11
    elseif Player.Exp.Value >= 6500 < 8000 then
        Player.leaderstats.PlayerLevel.Value = 12
    elseif Player.Exp.Value >= 8000 < 9500 then
        Player.leaderstats.PlayerLevel.Value = 13
    elseif Player.Exp.Value >= 9500 < 11000 then
        Player.leaderstats.PlayerLevel.Value = 14
    elseif Player.Exp.Value >= 11000 < 12500 then
        Player.leaderstats.PlayerLevel.Value = 15
    elseif Player.Exp.Value >= 12500 < 14000 then
        Player.leaderstats.PlayerLevel.Value = 16
    elseif Player.Exp.Value >= 14000 < 15500 then
        Player.leaderstats.PlayerLevel.Value = 17
    elseif Player.Exp.Value >= 15500 < 18000 then
        Player.leaderstats.PlayerLevel.Value = 18
    elseif Player.Exp.Value >= 18000 < 21000 then
        Player.leaderstats.PlayerLevel.Value = 19
    elseif Player.Exp.Value >= 21000 < 25000 then
        Player.leaderstats.PlayerLevel.Value = 20
    elseif Player.Exp.Value >= 25000 < 30000 then
        Player.leaderstats.PlayerLevel.Value = 21
    elseif Player.Exp.Value >= 30000 < 35000 then
        Player.leaderstats.PlayerLevel.Value = 22
    elseif Player.Exp.Value >= 35000 < 40000 then
        Player.leaderstats.PlayerLevel.Value = 23
    elseif Player.Exp.Value >= 40000 < 45000 then
        Player.leaderstats.PlayerLevel.Value = 24
    elseif Player.Exp.Value >= 45000 < 50000 then
        Player.leaderstats.PlayerLevel.Value = 25
    elseif Player.Exp.Value >= 50000 < 57500 then
        Player.leaderstats.PlayerLevel.Value = 26
    elseif Player.Exp.Value >= 57500 < 65000 then
        Player.leaderstats.PlayerLevel.Value = 27
    elseif Player.Exp.Value >= 65000 < 72500 then
        Player.leaderstats.PlayerLevel.Value = 28
    elseif Player.Exp.Value >= 72500 < 82500 then
        Player.leaderstats.PlayerLevel.Value = 29
    elseif Player.Exp.Value >= 82500 < 100000 then
        Player.leaderstats.PlayerLevel.Value = 30
    elseif Player.Exp.Value >= 100000 then
        Player.leaderstats.PlayerLevel.Value = 31
    else
    print("An error has occured with the LevelUp Function.")
    end

y = 100 * Player.leaderstats.Level.Value
Player.HP.Value = 200 + y
Player.Character:WaitForChild("Humanoid").MaxHealth = Player.HP.Value

end


2 answers

One) Incorrect conditions

You have the following written in your conditions:

-- (the following is incorrect)
if a >= b < c then

This is actually equivalent to

-- (the following is incorrect)
if a >= (b < c) then

Which is equivalent to

-- (the following is incorrect)
if a >= false then

which makes no sense since a number is not larger than false.


You need to explicitly give the second condition:

if a >= b and a < c then

Two) Dropping part of incorrect condition

HOWEVER, the >= check is unnecessary with the way you are using else! You just need the < check.

Three) Variables instead of repeating ourselves

We can also shorten this by replacing Player.Exp.Value in all of the conditions with a variable; we can shorten Player.leaderstats.PlayerLevel to a variable too. This had typos a few times. This is why you don't organize scripts like this!

Four) Default cases

Finally, your final condition should just be else rather than elseif xp then, and the 'error' cannot occur.

Full script (no loop / formula)

while wait() do
	Player = game.Players.LocalPlayer;
	local xp = Player.Exp.Value;
	local level = Player.leaderstats.PlayerLevel;
	if xp < 300 then
		level.Value = 2
	elseif xp < 600 then
		level.Value = 3
	elseif xp < 1000 then
		level.Value = 4
	elseif xp < 1500 then
		level.Value = 5
	elseif xp < 2000 then
		level.Value = 6
	elseif xp < 2500 then
		level.Value = 7
	elseif xp < 3000 then
		level.Value = 8
	elseif xp < 4000 then
		level.Value = 9
	elseif xp < 5000 then
		level.Value = 10
	elseif xp < 6500 then
		level.Value = 11
	elseif xp < 8000 then
		level.Value = 12
	elseif xp < 9500 then
		level.Value = 13
	elseif xp < 11000 then
		level.Value = 14
	elseif xp < 12500 then
		level.Value = 15
	elseif xp < 14000 then
		level.Value = 16
	elseif xp < 15500 then
		level.Value = 17
	elseif xp < 18000 then
		level.Value = 18
	elseif xp < 21000 then
		level.Value = 19
	elseif xp < 25000 then
		level.Value = 20
	elseif xp < 30000 then
		level.Value = 21
	elseif xp < 35000 then
		level.Value = 22
	elseif xp < 40000 then
		level.Value = 23
	elseif xp < 45000 then
		level.Value = 24
	elseif xp < 50000 then
		level.Value = 25
	elseif xp < 57500 then
		level.Value = 26
	elseif xp < 65000 then
		level.Value = 27
	elseif xp < 72500 then
		level.Value = 28
	elseif xp < 82500 then
		level.Value = 29
	elseif xp < 100000 then
		level.Value = 30
	else
		level.Value = 31
	end
	y = 100 * level.Value
	Player.HP.Value = 200 + y
	Player.Character:WaitForChild("Humanoid").MaxHealth = Player.HP.Value
end

Much shorter version using a table

local xps = {0,3, 6, 10, 15, 20, 25, 30, 40, 50,
	65, 80, 95, 110, 125, 140, 155, 180, 210,
	250, 300, 350, 400, 450, 500, 575, 650, 725,
	825, 1000};
-- Just a list (divided by 100 to be smaller)

while wait() do
	Player = game.Players.LocalPlayer;
	local xp = Player.Exp.Value;
	for level  = 1, #xps do
		if xp >= xps[level] * 100 then
			Player.leaderstats.PlayerLevel.Value = level;
		end
	end
	Player.HP.Value = 200 + 100 * Player.leaderstats.PlayerLevel.Value;
	Player.Character:WaitForChild("Humanoid").MaxHealth = Player.HP.Value
end

Final Concern with Character

I am unsure whether or not this will actually be an issue; in addition to making sure the Humanoid exists like you do in the final line inside the loop, you should probably also be sure that the Character is not nil as well (if Player.Character then { humanoid stuff } end)

2 comments

0

It still doesn't work, even though I literally copy and pasted the table codeblock you gave me. I did edit the final line of the loop like you asked, and that works fine, but the leveling does not.

0

What does "doesn't work" mean? Is there an error? Have you verified the names of the values?


-- Try this if you're not going to use tables
game.Players.PlayerAdded:connect(function()
while true do
wait()
if Player.leaderstats.PlayerLevel.Value >= 1 and Player.Exp.Value == 100 then
wait(1)
print(Player.Character.Name.." Has ranked up!")
elseif Player.leaderstats.PlayerLevel.Value <= 1 and Player.Exp.Value ==  >100 then
print(Player.Character.Name.."Doesn't have enough EXP!")
		end
	end
end)

2 comments

0

Tell me if it doesn't work, it should though

1

Wouldn't that only work for leveling up to 1 though?

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