I’m working on an RPG game, where you can complete quests and get rewards such as money and XP, when you get enough XP you can level up.
The problem is I am trying to create something where if you have enough XP you can level up.
It’s a script inside of ServerScriptService, no errors, and I probably did some obvious error I wouldn’t know about because it’s my first time trying to do something like this.
local MaxLevel = 15
game.Players.PlayerAdded:Connect(function(player)
while player.leaderstat.Level < MaxLevel do
if player.leaderstat.XP > player.leaderstats.Level * 49 then
player.leaderstats.Level = player.leaderstats.Level + 1
end
end
end)
Yeah, I forgot to put .Value. Even after putting those it wouldn’t work
local MaxLevel = 15
game.Players.PlayerAdded:Connect(function(player)
while player.leaderstat.Level.Value < MaxLevel do
wait()
if player.leaderstat.XP.Value > player.leaderstats.Level.Value * 49 then
player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
end
end
end)
local MaxLevel = 15
game.Players.PlayerAdded:Connect(function(player)
while player.leaderstats.Level.Value < MaxLevel do
wait()
if player.leaderstats.XP.Value > player.leaderstats.Level.Value * 49 then
player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
end
end
end)
I’m wondering, does it have to do with this?
if player.leaderstats.XP.Value > player.leaderstats.Level.Value * 49 then
It depends. Does the XP ever become greater than the level*49? You could achieve this by editing the values manually via the server (not the client). Try also adding some prints to see why it isn’t working.
I got rid of the maxlevel part, and just made it this and it works
game.Players.PlayerAdded:Connect(function(player)
while wait() do
if player.leaderstats.XP.Value > player.leaderstats.Level.Value * 49 then
print('working')
player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
end
end
end)
I use the following function whenever I add XP to the player:
function data:LevelUp()
while self.Data.XP >= Exponentials.XPNeeded(self.Data.Level) do
self.Data.XP = self.Data.XP - Exponentials.XPNeeded(self.Data.Level)
self.Data.Level = self.Data.Level + 1
end
self:Update() -- update is a function to change the leaderstats
end