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

LeaderStats won't change after getting the right amount of XP?

I need a leveling up system and this is pretty basic, but... it doesn't work.

game.Players.PlayerAdded:connect(function(player)
	stat = Instance.new("IntValue", player)
	stat.Name = "leaderstats"
	
	Gold = Instance.new("IntValue", stat)
	Gold.Name = "Gold"
	
	Exp = Instance.new("IntValue", stat)
	Exp.Name = "XP"
	
	Level = Instance.new("IntValue", stat)
	Level.Name = "Level"
	Level.Value = 1
	
	if Exp.Value == 4 * Level then
		Exp.Value = 0
		Level.Value = Level.Value +1
	end
	
end)

1 comment

0

For line 15 do: if Exp.Value == 4 * Level.Value

2 answers

✓ Accepted answer

Ok, so I'm guessing the leaderstats is created correctly, because I don't see an issue in that, but the problem is your if statement is only being called once after the stats are made.

game.Players.PlayerAdded:connect(function(player)
        stat = Instance.new("IntValue", player)
        stat.Name = "leaderstats"

        Gold = Instance.new("IntValue", stat)
        Gold.Name = "Gold"

        Exp = Instance.new("IntValue", stat)
        Exp.Name = "XP"

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

player.leaderstats.XP.Changed:connect(function() -- whenever the XP is changed
	if player.leaderstats.XP.Value >= 4*Level.Value then -- if the XP is equal to or more than 4 times the level
		player.leaderstats.XP.Value = 0 -- Reset the XP
		Level.Value = Level.Value + 1 -- Increase the level by one
	end

end)

end)

This way, the if statement will be checked every time the value changes instead of only when the leaderstats are first made.

If I have helped answer your question, please remember to accept my answer. Otherwise, please comment or message me on how I could have done better to help you.

This code will only run right as the player joins. You need it to run every time the XP amount changes.

The event for this is .Changed. This event will fire the code inside every time the XP is changed

Here is your revised code:

game.Players.PlayerAdded:connect(function(player)
    stat = Instance.new("IntValue", player)
    stat.Name = "leaderstats"

    Gold = Instance.new("IntValue", stat)
    Gold.Name = "Gold"

    Exp = Instance.new("IntValue", stat)
    Exp.Name = "XP"

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

	Exp.Changed:connect(function()
		if Exp.Value == 4 * Level then
			Exp.Value = 0
			Level.Value = Level.Value + 1
		end
	end)
end)

I hope this helped!

1 comment

0

It would be "4 * Level.Value" in line 16 btw. Also, use ">=" instead of "==" in the same line because the value could be 5 and level 1, and the level breaks in yours.

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