Ok so when the experience is greater than or equal to the needed experience, it is suppose to increase the players level by 1, and subtract the needed experience to level up from the experience. In other words, say the experience that is needed to level up is 2450, if the player had gained 2452 experience, this should result in a remainder of 2 experience points. What it does instead is go to -2448.
ServerScript
RemoteEvents.LevelCap.OnServerEvent:Connect(function(Player, Stat) local Data = Player.Persistance:WaitForChild("Data") if Data.Experience.Value >= Data.ExperienceNeeded.Value then wait(0.5) Data.Experience.Value = (Data.Experience.Value - Data.ExperienceNeeded.Value) wait(0.5) Data.ExperienceNeeded.Value = (Data.Level.Value * 50) Data.Level.Value = Data.Level.Value + 1 end end)
LocalScript
local Player = game.Players.LocalPlayer repeat wait() until Player.Character ~= nil local ReplicatedStorage = game:GetService("ReplicatedStorage") local RemoteEvents = ReplicatedStorage.RemoteEvents local Data = Player.Persistance:WaitForChild("Data") local pcall function StatChange() RemoteEvents.LevelCap:FireServer("Experience") print("Hello") end Data.Experience.Changed:Connect(StatChange)
it does level up the player, but the experience just inverts.
Hi, I'm BlackOrange and I will be helping you out today!
Alright, so what I would do is make a function that checks if the EXP is more then or less then and then add the rest of the exp.
So I suggest making a level up function:
local function LevelUp() -- change current exp to 0 -- update max exp needed to level up -- Update player level (add one) end local function CheckRemainderExp() if EXPGained > ExpNeededToLevelUp.Value then local Difference = EXPGained - EXPNeededToLevelUp.Value LevelUp() CurrentExp.Value = CurrentExp.Value + Difference -- this will add remainder after level up end end
You need to update the variables
This is only a template. So you reset everything and then add remainder. This is a efficient way to do this so you don't need to check values. Of course you will need more conditional statements to make this better. Again this is only a template
Hopefully this template helped.
Best of luck developer!