So, everything in my datastore works perfectly. However if my xp is 1000 or higher it won’t save the xp and converts it to a level. This is extremely bad since players could farm levels.
-- Datastore Variables
local DataStoreService = game:GetService("DataStoreService")
local LvlCreditsDatastore = DataStoreService:GetDataStore("LvlCreditsDataStore")
local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(Player)
-- Leaderstats Folder
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
-- Level Stat
local Level = Instance.new("NumberValue", leaderstats)
Level.Name = "Level"
Level.Value = 1
-- XP Stat
local XP = Instance.new("NumberValue", leaderstats)
XP.Name = "XP"
XP.Value = 0
-- Required XP
local RequiredXP = Instance.new("NumberValue", Player)
RequiredXP.Name = "RequiredXP"
RequiredXP.Value = 1000
-- Required Levels (For Prestige)
local RequiredLevels = Instance.new("NumberValue", Player)
RequiredLevels.Name = "RequiredLevels"
RequiredLevels.Value = 100
-- Prestige Stat
local Prestige = Instance.new("NumberValue", leaderstats)
Prestige.Name = "Prestige"
Prestige.Value = 0
-- Credits
local Credits = Instance.new("IntValue")
Credits.Name = "Credits"
Credits.Parent = leaderstats
local playerUserID = "Player_"..Player.UserId
------------------------------------------------------------------------------------
-- Level Up System
XP.Changed:Connect(function(Changed)
if XP.Value >= RequiredXP.Value then
XP.Value = 0
Level.Value += 1
end
end)
-- Prestige Leveling System
Level.Changed:Connect(function(Changed)
if Level.Value >= RequiredLevels.Value and Prestige.Value < 10 then
Level.Value = 1
Prestige.Value += 1
RequiredXP.Value += 1000
end
end)
-------------------------------------------------------------------------------------
-- Datastore Variables
local CreditsData = LvlCreditsDatastore:GetAsync(playerUserID)
local data
--If player has never gotten credits
if CreditsData == nil then
Credits.Value = 0
RequiredXP.Value = 1000
LvlCreditsDatastore:SetAsync(playerUserID, CreditsData)
end
--Load data
local success, errormessage = pcall(function()
data = LvlCreditsDatastore:GetAsync(playerUserID)
end)
if success then
Credits.Value = data.Credits
Level.Value = data.Level
XP.Value = data.XP
RequiredXP.Value = data.RequiredXP
RequiredLevels.Value = data.RequiredLevels
Prestige.Value = data.Prestige
end
end)
------------------------------------------------------------------------------------
-- DATASTORE SAVING
game.Players.PlayerRemoving:Connect(function(Player)
-- Save Data
local playerUserID = "Player_"..Player.UserId
local data = {
Credits = Player.leaderstats.Credits.Value;
Level = Player.leaderstats.Level.Value;
XP = Player.leaderstats.XP.Value;
RequiredXP = Player.RequiredXP.Value;
RequiredLevels = Player.RequiredLevels.Value;
Prestige = Player.leaderstats.Prestige.Value;
}
local success, errormessage = pcall(function()
LvlCreditsDatastore:SetAsync(playerUserID, data)
end)
if success then
print("Data saved successfully")
else
print("Error in saving data")
warn(errormessage)
end
end)
It looks like the issue lies in the Level Up System. When the player’s XP reaches the RequiredXP value, the XP is reset to 0 and the player’s Level is increased by 1. However, if the player’s XP is already at 1000 or higher, it will skip the if statement and not reset the XP value. To fix this, you can add an extra check to make sure the XP value is not already greater than or equal to the RequiredXP value before resetting it.
Here’s an updated version of the Level Up System that should work:
-- Level Up System
XP.Changed:Connect(function(Changed)
if XP.Value >= RequiredXP.Value then
local remainingXP = XP.Value - RequiredXP.Value
XP.Value = remainingXP < 0 and 0 or remainingXP
Level.Value += 1
end
end)
This version checks if the XP value is greater than or equal to the RequiredXP value, and if so, it subtracts the RequiredXP value from the XP value to get the remaining XP. Then, it sets the XP value to the remaining XP (which will be 0 if it was less than the RequiredXP value), and increases the player’s Level by 1.
So, the required xp per level is 1000 if your prestige = 0. When you get 100 levels you prestige, your level is reset to 1, and your required xp is +1000. I was testing this on prestige 1 so I could make sure it works since the required xp per level at prestige 1 is 2000. I got myself up to 1,200 xp on prestige 1 and instead of saving that 1,200 xp it just saved 200 and gave me a level despite you needing 2000 xp to level up at prestige 1
Wait I fixed it. So instead of defining the value for required xp at the top of the script with the rest of the leaderstats I defined it in the -if player has never gotten credits section. So basically if a player has no data their required xp amount is 1000
Certainly! Let me explain some more. So this code that I gave:
XP.Changed:Connect(function(Changed)
if XP.Value >= RequiredXP.Value then
local remainingXP = XP.Value - RequiredXP.Value
XP.Value = remainingXP < 0 and 0 or remainingXP
Level.Value += 1
end
end)
This script represents the Level Up System in your game. The XP.Changed event is triggered whenever the XP value changes, and it calls the function that follows. This function has one parameter, Changed, which is the new value of XP.
The first line of the function checks whether the player’s XP value is greater than or equal to the RequiredXP value. If it is, it means that the player has earned enough experience points to level up.
The next line calculates the remaining XP that the player has after reaching the required amount. It does this by subtracting the RequiredXP value from the XP value.
The third line sets the XP value to either 0 (if the remaining XP is negative) or the remaining XP (if it is non-negative).
The final line increases the player’s Level by 1.
By checking whether the XP value is already greater than or equal to the RequiredXP value, this updated code ensures that the player’s XP value will be correctly reset to 0 when they level up, regardless of whether their XP value was already at or above the required amount.