playerdata = {}
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
local maxlevel = 50
game.Players.PlayerAdded:connect(function(player)
mydata = datastore:GetAsync(player.userId) or {}
mydata.level = mydata.level or 1
mydata.xp = mydata.xp or 0
mydata.wins = mydata.wins or 0
playerdata[player.userId] = mydata
local leaderstats = player:FindFirstChild("leaderstats")
local newLeaderstats = Instance.new("Model", player)
newLeaderstats.Name = "leaderstats"
local Level = Instance.new("IntValue", newLeaderstats)
Level.Name = "Level"
Level.Value = mydata.level
local Wins = Instance.new("IntValue", player)
Wins.Name = "Wins"
Wins.Value = mydata.wins
local XP = Instance.new("IntValue", player)
XP.Name = "XP"
XP.Value = mydata.xp
while wait() do
local extralevel = Level.Value
local xpneeded = ((extralevel * 2) - 1) * 40
if Level.Value < maxlevel then
if XP.Value >= xpneeded then
Level.Value = mydata.level + 1
mydata.level = Level.Value
XP.Value = mydata.xp - xpneeded
mydata.xp = XP.Value
end
end
end
end)
On the odd occasion, when a player levels up, the XP seems to go to negative, for example, if you are Lv 2, you need 120 XP to level up to Lv.3, and if the game gave you 130XP, it would level you up to Lv.3 and you would keep 10XP, but instead of it taking 120 XP from the player, it seems to take 120XP, but make their leftover XP to negative.
Their are no errors in the output, and it looks perfectly fine to me.
This error only occurs occasionally.

1 comment
You should be using a changed event, not an infinite loop.