My level exp keeps resetting

I’m trying to make a system where a player levels up(they are getting constant exp for testing purposes)
but whenever the exp that is needed updates, it goes back to 100. Its suppose to get the level and multiply it by 100, it does that but it just resets back to 100 after a second or two

Client:

local clipping = script.Parent:WaitForChild("BarClipping")
clipping.ClipsDescendants = true

local expTxt = script.Parent:WaitForChild("Experience")
local levelTxt = script.Parent:WaitForChild("Level")

local expVal = game.Players.LocalPlayer:WaitForChild("Stats"):WaitForChild("Experience")
local levelVal = game.Players.LocalPlayer.Stats:WaitForChild("Level")


local previousExp = expVal.Value


function updateGui()

	local neededExp = math.floor(levelVal.Value * 100)
	local previousLevelExp = math.floor((levelVal.Value - 1) *100)
	local expDiff = neededExp - previousLevelExp
	local currentExp = expVal.Value - previousLevelExp
	
	
	local x = currentExp / expDiff

	clipping.Size = UDim2.new(x, 0, 1, 0)
	clipping.Bar.Size = UDim2.new(1 / x, 0, 1, 0)


	expTxt.Text = currentExp .. "/" .. expDiff
	levelTxt.Text = "Level " .. levelVal.Value


	if expVal.Value >= neededExp then
		local nextExpNeeded = math.floor((levelVal.Value + 1)* 100)
		expTxt.Text = "0/" .. nextExpNeeded
		levelTxt.Text = "Level " .. levelVal.Value + 1
		clipping.Size = UDim2.new(0, 0, 1, 0)
	end
	
	previousExp = expVal.Value
end


updateGui()

expVal:GetPropertyChangedSignal("Value"):Connect(updateGui)

Server:

local dss = game:GetService("DataStoreService")
local levelDS = dss:GetDataStore("Levels")



function incrementExp(player, increment)

	for i = player.Stats.Experience.Value, player.Stats.Experience.Value + increment do

		player.Stats.Experience.Value = i

		wait()
	end
end


function saveData(player)

	pcall(function()

		local level = player.Stats.Level.Value
		local exp = player.Stats.Experience.Value

		levelDS:SetAsync(player.UserId .. "Level", {level, exp})
	end)
end


game.Players.PlayerAdded:Connect(function(player)


	local statsFolder = Instance.new("Folder", player)
	statsFolder.Name = "Stats"

	local levelVal = Instance.new("IntValue", statsFolder)
	levelVal.Name = "Level"
	levelVal.Value = 1

	local expVal = Instance.new("IntValue", statsFolder)
	expVal.Name = "Experience"


	pcall(function()

		local data = levelDS:GetAsync(player.UserId .. "-Level")

		if data then

			levelVal.Value = data[1]
			expVal.Value = data[2]
		end
	end)


	expVal:GetPropertyChangedSignal("Value"):Connect(function()
		local neededExp = math.floor(levelVal.Value * 100) 
		if expVal.Value >= neededExp then
			levelVal.Value += 1
		end
	end)
	

	while wait(0.2) do
		incrementExp(player, 20)
	end
end)


game.Players.PlayerRemoving:Connect(saveData)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		saveData(player)
	end
end)

video of it happening:

It is always 100 because this math method will always be 100

local neededExp = math.floor(levelVal.Value * 100)
local previousLevelExp = math.floor((levelVal.Value - 1) *100)
local expDiff = neededExp - previousLevelExp

Let me give you an example of this mathematical method:
So let’s say X is neededExp and Y is previousLevelExp, X = 200, Y = 100. if you use expDiff math, you’ll get 100. Now for another example, let’s say you level up, now it would be X = 300, Y = 200, again 100

So in the line 30

expTxt.Text = currentExp .. "/" .. expDiff

It is showing this result. You can change the math method to this:

local neededExp = math.floor(levelVal.Value * 100)
local TotalExpUI = neededExp 

local x = currentExp / TotalExpUI

clipping.Size = UDim2.new(x, 0, 1, 0)
clipping.Bar.Size = UDim2.new(1 / x, 0, 1, 0)


expTxt.Text = currentExp .. "/" .. TotalExpUI

You don’t need to do math if you already have the answer! :grin:

1 Like

im not sure if i implemented it right but it starts at -200 or so and goes to 0 and then starts working but it
always starts at neededExp and ends at 0.
client part i changed:

function updateGui()
	
	local previousLevelExp = math.floor((levelVal.Value + 1)*100)
	local currentExp = expVal.Value
	local neededExp = math.floor(levelVal.Value * 100)
	local TotalExpUI = neededExp 

	local x = currentExp / TotalExpUI

	clipping.Size = UDim2.new(x, 0, 1, 0)
	clipping.Bar.Size = UDim2.new(1 / x, 0, 1, 0)


	expTxt.Text = currentExp .. "/" .. TotalExpUI
	levelTxt.Text = "Level " .. levelVal.Value


	if expVal.Value == neededExp then
		local nextExpNeeded = (math.floor((levelVal.Value + 1)*100)) - neededExp
		expTxt.Text = "0/" .. nextExpNeeded
		levelTxt.Text = "Level " .. levelVal.Value + 1
		clipping.Size = UDim2.new(0, 0, 1, 0)
	end
	
	previousExp = expVal.Value
end

client part i changed

expVal:GetPropertyChangedSignal("Value"):Connect(function()
		local neededExp = math.floor((levelVal.Value + 1)*100) 
		if expVal.Value >= neededExp then
			levelVal.Value += 1
		end
	end)

ill have to settle with it going up every 2 levels but thats fine, thanks for helping!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.