I am still in the stages of learning and I am working on a leaderstats board with an XP leveling system. Right now everything is working minus the leveling system. The leveling system works up to when you get enough XP to reach level 1, after that the player level goes up with every kill. It doesn’t go up based off XP earned.
My goal was to figure out how to add 100 required XP every time the players levels up. Basically have 150 XP required for level 1 and then level 2 would be 250, level 3 be 350 etc. The XP should reset to zero at every level up. I have attached the code I am working with below.
leaderstats code
local function addBoard(player)
local board = Instance.new("Folder", player)
board.Name = "leaderstats"
local level = Instance.new("IntValue", board)
level.Name = "Level"
level.Value = 0
local kills = Instance.new("IntValue", board)
kills.Name = "Kills"
local xp = Instance.new("IntValue", board)
xp.Name = "XP"
xp.Value = 0
end
local function onCharAdded(char)
local hum = char:WaitForChild("Humanoid")
hum.Died:Connect(function()
local tag = hum:FindFirstChild("creator")
if tag then
local ePlayer = tag.Value
local kills = ePlayer.leaderstats.Kills
kills.Value = kills.Value + 1
local xp = ePlayer.leaderstats.XP
xp.Value = xp.Value + 10
end
end)
end
local function onPlayerAdded(player)
addBoard(player)
player.CharacterAdded:Connect(onCharAdded)
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
StarterPack has this localscript in it
local plr = game.Players.LocalPlayer
plr.leaderstats.XP.Changed:connect(function()
if plr.leaderstats.XP.Value > 149 then -- If they have more than 49 xp (50 +)
plr.leaderstats.Level.Value = plr.leaderstats.Level.Value +1 -- Add one to their levels
plr.leaderstats.XP.Value = 0 -- Reset player's xp to 0
end
end)
Your code is working when using it in the localscript in the StarterPack folder. I only notcied one thing with it though. When the player levels up the XP resets to zero and on the first kill the XP goes right back where it was. Basically player has 50 XP and levels up and now has 0 XP. The first kill throws them back to 60 XP.
You could make a variable for the XP Required to level up and then increase the XP Required by 100 every time they reach the goal of the XP Required, so do something like this:
local xpRequirement = 100
while true do -- checks every 1 second
wait(1)
if plr.leaderstats.XP.Value >= xpRequirement then
plr.leaderstats.Level.Value += 1
xpRequirement += 100
plr.leaderstats.XP.Value = 0
end
end
I know that code has to be input in a certain order so that it runs correctly, I believe and correct me if I am wrong on that. So where would I insert what you shared above in the leaderstats? I tried it earlier and it didn’t run .
I keep getting attempt tp index nil with that in the leaderstats code.
it is the plr part that is throwing that error in the output window. Not sure why but I am trying to figure it out.
Add the code right in the game.Players.PlayerAdded event.
local plr = player
plr.leaderstats.XP.Changed:connect(function()
if plr.leaderstats.XP.Value > 149 then -- If they have more than 49 xp (50 +)
plr.leaderstats.Level.Value = plr.leaderstats.Level.Value +1 -- Add one to their levels
plr.leaderstats.XP.Value = 0 -- Reset player's xp to 0
end
end)