Hi, I'm trying to make a leveling system server script into a game I'm building to work and I got it to where the stats and all will load onto the player but the trouble I'm having is I added a loop block for whenever player XP changes for the game to check if it's more or less than the XP needed to level up and if more to then check player level against maxlevel and if max level then nil but if below max level then level up however anytime I change my XP to higher than XP Needed no level up happens. I've tried writing it close to ten different ways now and honestly I'm relatively deficient in my LUA vocabulary so I apologize if it's some really simple error that I should've been aware of, if it is then I'll make sure to not let it happen again, but I kinda need someone to tell me my mistake before I can learn from my mistakes, so all help appreciated!
local DS = game:GetService("DataStoreService")
local LevelSave = DS:GetDataStore("LevelSaveSystem")
local XPSave = DS:GetDataStore("XPSaveSystem")
local GoldSave = DS:GetDataStore("GoldSaveSystem")
game.Players.PlayerAdded:Connect(function(Player)
local Folder = Instance.new("Folder",Player)
Folder.Name = "leaderstats"
local Level = Instance.new("DoubleConstrainedValue",Folder)
Level.Name = "Level"
Level.MaxValue = 100
Level.MinValue = 1
Level.Value = LevelSave:GetAsync(Player.UserId) or 1
Level.Changed:Connect(function()
LevelSave:SetAsync(Player.UserId, Level.Value)
end)
local XP = Instance.new("IntValue",Folder)
XP.Name = "XP"
XP.Value = XPSave:GetAsync(Player.UserId) or 0
XP.Changed:Connect(function()
XPSave:SetAsync(Player.UserId, XP.Value)
end)
local Gold = Instance.new("IntValue",Folder)
Gold.Name = "Gold"
Gold.Value = GoldSave:GetAsync(Player.UserId) or 0
Gold.Changed:Connect(function()
GoldSave:SetAsync(Player.UserId, Gold.Value)
end)
local XPN = Instance.new("IntValue",Folder)
XPN.Name = "XPN"
XPN.Value = (math.floor((((8 * Level.Value) ^ 1.75) / 16) + 8.5))
end)
while true do
XP.Value.Changed:Connect(function()
if XP.Value < XPN.Value or 0 then return end
if XP.Value >= XPN.Value then
if Level.Value < Level.MaxValue then
local Humanoid = Player.Character:WaitForChild("Humanoid")
XP.Value = XP.Value - XPN.Value
Level.Value = Level.Value + 1
Humanoid.MaxHealth = Humanoid.MaxHealth + 5
Humanoid.Health = (Humanoid.MaxHealth * 0.90)
end
if Level.Value >= Level.MaxValue then return end
end
end)
end
game.Players.PlayerRemoving:Connect(function(Player)
LevelSave:SetAsync(Player.UserId, Player.leaderstats.Level.Value)
XPSave:SetAsync(Player.UserId, Player.leaderstats.XP.Value)
GoldSave:SetAsync(Player.UserId, Player.leaderstats.Gold.Value)
end)
Sorry again if it ends up a dumb question :/
20:21:11.387 - ServerScriptService.Script:39: attempt to index global 'XP' (a nil value) 20:21:11.388 - Stack Begin 20:21:11.388 - Script 'ServerScriptService.Script', Line 39 20:21:11.389 - Stack End

1 comment
Add a wait() to your loop, because you're making an inf loop which causes your studio to crash.