Help with Level system

My level script isn’t working, please help.
Script:

local expModule = require(script.Parent.ExpModule)

local Table = {}

local function updateLevel(plr,level,experience,maxexperience)
	while true do
		if experience >= maxexperience then
			level += 1
			experience -= maxexperience
			maxexperience = expModule:GetMaxExperience(level)
		else
			Table[plr] = false
			break
		end
		wait()
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	local exp = plr:WaitForChild("Exp")
	local level = plr.Level
	local maxExp = plr.MaxExp
	exp.Changed:Connect(function()
		if not Table[plr] then
			Table[plr] = true  
			updateLevel(plr,level.Value,exp.Value,maxExp.Value)
		end
	end)
end)

One of the main reasons why your Script isn’t working is that, You’re running a while true do loop, Meaning that it will never proceed (Plus, because you’re running it whenever the EXP value changes) And also because you’re using .Value, Meaning that you will only get the value, But will not be able to modify it.

You don’t need to add a while loop inside the function, You can simply run it whenever the EXP value changes. I have tried fixing it for you, Let me know if it works!

local Players = game:GetService("Players")
local expModule = require(script.Parent.ExpModule)
local Table = {}

local function updateLevel(plr, level, experience, maxexperience)
	if experience.Value >= maxexperience.Value then
		level.Value += 1
		experience.Value -= maxexperience.Value
		maxexperience.Value = expModule:GetMaxExperience(level.Value)
	else
		Table[plr] = false
	end
end

Players.PlayerAdded:Connect(function(plr)
	local exp = plr:WaitForChild("Exp")
	local level = plr.Level
	local maxExp = plr.MaxExp

	exp:GetPropertyChangedSignal("Value"):Connect(function()
		if not Table[plr] then
			Table[plr] = true  
			updateLevel(plr, level, exp, maxExp)
		end
	end)
end)

Thank you so much dude! You saved my game!

1 Like