You can write your topic however you want, but you need to answer these questions:
-
i can’t seem to fix the leveling system added, whenever i gain 100 xp for example, i should level up to the “required” amount of xp needed to level
-
the issue is, when i start from level 0, and then i gain 100 xp, it does not level me up at all no matter what i do i can’t seem to make it level me up to the next level
-
i tried changing the updateLevel function and no matter what i change it to, it just does not work at all i don’t know what to do and i do think the issue is in that area but i’m not even sure at this point it’s confusing
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- local rs = game.ReplicatedStorage:WaitForChild("QuestsReplicatedStorage")
local remotes = rs:WaitForChild("RemoteEvents")
local questModules = rs:WaitForChild("QuestModules")
-- XP ranges needed for each level
-- XP ranges needed for each level
local xpRanges = {
[0] = 1, -- Starting level is 0, and the required XP to reach level 1 is 0.
[1] = 50, -- The required XP to reach level 2 is 50.
[2] = 100, -- The required XP to reach level 3 is 100.
[3] = 200, -- The required XP to reach level 4 is 200.
-- Add more levels and XP ranges as needed
}
-- Data saving
local dss = game:GetService("DataStoreService")
local datastore = dss:GetDataStore("DATA")
function giveXP(player, amount)
local xpValue = player.leaderstats.XP
xpValue.Value = xpValue.Value + amount
print(player.Name .. " gained " .. amount .. " XP.")
updateLevel(player) -- Call updateLevel after gaining XP to check for level advancement
end
local function dropXPOrbs(position, xpAmount)
local blueOrb = Instance.new("Part")
blueOrb.Name = "XP_Orb"
blueOrb.Anchored = true
blueOrb.CanCollide = false
blueOrb.Shape = Enum.PartType.Ball
blueOrb.BrickColor = BrickColor.new("Bright blue")
blueOrb.Size = Vector3.new(1, 1, 1)
blueOrb.Position = position
blueOrb.Parent = workspace
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = Vector3.new(math.random(-5, 5), 10, math.random(-5, 5))
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVelocity.Parent = blueOrb
wait(2) -- Change this delay to the time the orb should stay before disappearing
blueOrb:Destroy()
end
function updateLevel(player)
local xpValue = player.leaderstats.XP.Value
local currentLevel = player.leaderstats.Level.Value
-- This loop ensures that the player can level up multiple times if they have enough XP
repeat
local nextLevel = currentLevel + 1
local xpNeededForNextLevel = xpRanges[nextLevel]
print("Updating level for " .. player.Name .. "...")
print("Current Level: " .. currentLevel)
print("XP Value: " .. xpValue)
print("XP Needed For Next Level: " .. xpNeededForNextLevel)
-- Check if the player has enough XP to reach the next level or if they have overgained
if xpValue >= xpNeededForNextLevel then
-- Level up
player.leaderstats.Level.Value = nextLevel
-- Calculate the excess XP gained beyond the requirement
xpValue = xpValue - xpNeededForNextLevel
currentLevel = nextLevel
else
break -- Exit the loop if the player doesn't have enough XP to level up further
end
until xpValue < xpRanges[currentLevel + 1]
-- Update the player's XP to the remaining amount
player.leaderstats.XP.Value = xpValue
print(player.Name .. " leveled up to Level " .. player.leaderstats.Level.Value)
end
function saveData(plr)
if not plr:FindFirstChild("DATA FAILED TO LOAD") then
local cash = plr.leaderstats.Cash.Value
local xp = plr.leaderstats.XP.Value
local completedQuests = {}
for _, quest in pairs(plr["COMPLETED QUESTS"]:GetChildren()) do
table.insert(completedQuests, quest.Name)
end
local currentQuests = {}
for _, quest in pairs(plr["CURRENT QUESTS"]:GetChildren()) do
table.insert(currentQuests, { quest.Name, quest.PROGRESS.Value })
end
local compiledData = {
CASH = cash;
XP = xp;
COMPLETED_QUESTS = completedQuests;
CURRENT_QUESTS = currentQuests;
}
local success, err = nil, nil
while not success do
success, err = pcall(function()
datastore:SetAsync(plr.UserId, compiledData)
end)
if err then
warn(err)
end
task.wait(0.02)
end
end
end
game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
saveData(plr)
end
end)
game.Players.PlayerAdded:Connect(function(plr)
local dataFailedWarning = Instance.new("StringValue")
dataFailedWarning.Name = "DATA FAILED TO LOAD"
dataFailedWarning.Parent = plr
local success, plrData = nil, nil
while not success do
success, plrData = pcall(function()
return datastore:GetAsync(plr.UserId)
end)
task.wait(0.02)
end
dataFailedWarning:Destroy()
if not plrData then
plrData = {
CASH = 0;
XP = 0;
COMPLETED_QUESTS = {};
CURRENT_QUESTS = {};
}
end
local ls = Instance.new("Folder")
ls.Name = "leaderstats"
ls.Parent = plr
local cashVal = Instance.new("IntValue")
cashVal.Name = "Cash"
cashVal.Value = plrData.CASH
cashVal.Parent = ls
local xpVal = Instance.new("IntValue")
xpVal.Name = "XP"
xpVal.Value = plrData.XP
xpVal.Parent = ls
-- Add the "Level" stat
local levelVal = Instance.new("IntValue")
levelVal.Name = "Level"
levelVal.Value = 0 -- Starting level is 0
levelVal.Parent = ls
print(plr.Name .. " joined with Level " .. levelVal.Value)
local completedQuestsFolder = Instance.new("Folder")
completedQuestsFolder.Name = "COMPLETED QUESTS"
completedQuestsFolder.Parent = plr
for _, completedQuest in pairs(plrData.COMPLETED_QUESTS) do
local questValue = Instance.new("StringValue")
questValue.Name = completedQuest
questValue.Parent = completedQuestsFolder
end
local currentQuestsFolder = Instance.new("Folder")
currentQuestsFolder.Name = "CURRENT QUESTS"
currentQuestsFolder.Parent = plr
for _, currentQuest in pairs(plrData.CURRENT_QUESTS) do
local questModuleScript = require(questModules[currentQuest[1]])
questModuleScript.AddQuest(plr, currentQuest[2])
end
ls.Parent = plr
-- Call the updateLevel function after setting up the leaderstats folder
updateLevel(plr)
end)
-- Quest handling
local npcs = workspace:WaitForChild("QUEST NPCS")
for _, npc in pairs(npcs:GetChildren()) do
local questName = npc["QUEST INFO"].QuestToGive.Value
local pp = Instance.new("ProximityPrompt")
pp.ObjectText = npc.Name
pp.ActionText = "Talk to " .. npc.Name
pp.HoldDuration = 0
pp.Parent = npc
pp.Triggered:Connect(function(plr)
if not plr["CURRENT QUESTS"]:FindFirstChild(questName) then
remotes:WaitForChild("PromptQuest"):FireClient(plr, npc)
end
end)
end
remotes:WaitForChild("QuestAccepted").OnServerEvent:Connect(function(plr, questName)
if questName and questModules:FindFirstChild(questName) and not plr["COMPLETED QUESTS"]:FindFirstChild(questName) and not plr["CURRENT QUESTS"]:FindFirstChild(questName) then
local questModuleScript = require(questModules[questName])
questModuleScript.AddQuest(plr)
end
end)
-- Inside the code that handles when an enemy is killed
-- Add this part to handle xp gain and xp orbs
function handleEnemyKilled(player, xpAmount)
giveXP(player, xpAmount)
dropXPOrbs(player.Character.HumanoidRootPart.Position, xpAmount)
end