ModuleScript sees xp equal to maxXp

I’m trying to make my own XP system, but every time module:increase() is called, it would add another level to the player, even if their XP is not equal to maxXp

local module = {}
--local datastoreservice = require(game:GetService('ReplicatedStorage').Services.DataStore2)
--datastoreservice:Combine('xpkey9#$1','level','xp','maxXp')
local datastoreservice = game:GetService('DataStoreService')
local datastore = datastoreservice:GetDataStore('xpSystemDatastore')
local key = 'xp9Datastore!Key'
local players = game:GetService('Players')
local data = {
	['level'] = 1,
	['xp'] = 0,
	['maxXp'] = 5
}

local function checkIfData(player,value:string)
	local success, output = pcall(function()
		local datastore1 = datastore:GetAsync(player.UserId..key)
	end)
	if success then
		if output then
			return output[value]
		else
			return data[value]
			
		end
	else
		warn(output)
	end
end

function module:startModule(player)
	warn('XPservice is being loaded')
	--local xpdata = datastoreservice('xp', player)
	local xp = Instance.new('NumberValue')
	xp.Name = 'xp'
	xp.Value = checkIfData(player,'xp')
	--xp.Value = xpdata:Get(0)
	xp.Parent = player
	
	--local leveldata = datastoreservice('level', player)
	local level = Instance.new('NumberValue')
	level.Name = 'level'
	level.Value = checkIfData(player,'level')
	--level.Value = leveldata:Get(1)
	level.Parent = player
	
	--local leveldata = datastoreservice('maxXp', player)
	local max = Instance.new('NumberValue')
	max.Name = 'maxXp'
	max.Value = checkIfData(player,'maxXp')
	--level.Value = leveldata:Get(5)
	max.Parent = player
end


local function autosave(player)
	local valueTable = {
		['XP'] = player:FindFirstChild('xp').Value,
		['Level'] = player:FindFirstChild('level').Value,
		['MaxXp'] = player:FindFirstChild('maxXp').Value
	}
	
	datastore:SetAsync(player.UserId..key,valueTable)
end

function module:getValues(player)
	local instanceTable = {
		['XP'] = player:FindFirstChild('xp'),
		['Level'] = player:FindFirstChild('level'),
		['MaxXp'] = player:FindFirstChild('maxXp')
	}
	
	return instanceTable
end

function module:increase(amount, player)
	local amountCount = amount
	local instanceTable = {
		['XP'] = player:FindFirstChild('xp'),
		['Level'] = player:FindFirstChild('level'),
		['MaxXp'] = player:FindFirstChild('maxXp')
	}
	
	while task.wait() do
		warn(amountCount)
		
		amountCount -= 1
		instanceTable.XP.Value += 1
		if instanceTable.XP.Value <= instanceTable.MaxXp.Value then
			warn('new lev wow')
			instanceTable.Level.Value += 1
			--instanceTable.XP.Value += 1
			instanceTable.MaxXp.Value += 5
			--amountCount -= 1

			if amountCount <= 0 then
				break
			end
		elseif amountCount <= 0 then
			break
		end
	end
	autosave(player)
end

function module:decrease(amount, player)
	local amountCount = amount
	local instanceTable = {
		['XP'] = player:FindFirstChild('xp'),
		['Level'] = player:FindFirstChild('level'),
		['MaxXp'] = player:FindFirstChild('maxXp')
	}

	while task.wait() do
		if instanceTable.XP.Value <= 0 then
			instanceTable.Level.Value -= 1
			instanceTable.XP.Value -= 1
			instanceTable.MaxXp.Value -= 5
			amountCount -= 1

			if amountCount <= 0 then
				break
			end
		elseif amountCount <= 0 then
			break
		end
		amountCount -= 1
		instanceTable.XP.Value -= 1
	end
	autosave(player)
end

function module:set(amount, player, selected)
	local instanceTable = {
		['XP'] = player:FindFirstChild('xp'),
		['Level'] = player:FindFirstChild('level'),
		['MaxXp'] = player:FindFirstChild('maxXp')
	}
	
	if selected == 'XP' then
		module:increase(amount,player)
	elseif selected == 'LEVEL' then
		instanceTable['Level'].Value = amount
	elseif selected == 'MAX_XP' then
		instanceTable['MaxXp'].Value = amount
	end
	
	autosave(player)
end

return module

You’re checking if the XP is less or equal to the MaxXp, did you mean to do ==?

2 Likes

I mistaken <= for == lol (auhwdgjawgdjyhawgdawd)

1 Like