Attemp to compare nil <= number

as you can see on the screenshot, there is this error: ‘Attemp to compare nil <= number’

the problem is that i cannot found the error… Searched on multiple posts but nothing worked, can anybody help, here is my script:

local TweenService = game:GetService("TweenService")
local ConfigurationXP = require(game:GetService("ReplicatedStorage").Configuration.XP)
local changeDatas = require(game:GetService("ReplicatedStorage").Modules.ChangeDataValue)
local player = game.Players.LocalPlayer

local function calculBar()
	if player.DS.Levels.Value then
		if player.DS.EXP.Value >= ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)] then
			changeDatas.removeValue(player.DS.EXP, ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)])
			changeDatas.addValue(player.DS.Levels, 1)
			
			local calcul = ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)] - player.DS.EXP.Value
			TweenService:Create(script.Parent.Level.Bar, TweenInfo.new(0.5), {Size = UDim2.new(1 / calcul, 0, 1, 0)}):Play()
			script.Parent.Level.Label.Text = "LEVEL " .. tostring(player.DS.Levels.Value) .. " · " .. tostring(player.DS.EXP.Value) .. " EXP"

		end
	end
end

calculBar()
player.DS.EXP:GetPropertyChangedSignal('Value'):Connect(calculBar)

Hey, I think you should add an if statement that if the value of the concerned variable is nil, replace it with a value of 0:

local TweenService = game:GetService("TweenService")
local ConfigurationXP = require(game:GetService("ReplicatedStorage").Configuration.XP)
local changeDatas = require(game:GetService("ReplicatedStorage").Modules.ChangeDataValue)
local player = game.Players.LocalPlayer

local function calculBar()
	if player.DS.Levels.Value == nil then
		player.DS.Levels.Value = 0
	end

	if player.DS.Levels.Value then
		if player.DS.EXP.Value >= ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)] then
			changeDatas.removeValue(player.DS.EXP, ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)])
			changeDatas.addValue(player.DS.Levels, 1)
			
			local calcul = ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)] - player.DS.EXP.Value
			TweenService:Create(script.Parent.Level.Bar, TweenInfo.new(0.5), {Size = UDim2.new(1 / calcul, 0, 1, 0)}):Play()
			script.Parent.Level.Label.Text = "LEVEL " .. tostring(player.DS.Levels.Value) .. " · " .. tostring(player.DS.EXP.Value) .. " EXP"

		end
	end
end

calculBar()
player.DS.EXP:GetPropertyChangedSignal('Value'):Connect(calculBar)

Are you sure that ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)] isn’t nil?

1 Like

I will test that please give me a minute

It’s working but I have to modify the script you sent me, i’ve also created a module script to change value of a intvalue:

local module = {}

function module.addValue(path, number:number) path.Value += number; end
function module.removeValue(path, number:number) path.Value -= number; end
function module.setValue(path, number:number) path.Value = number; end

return module

I used this module to set the value of the levels ‘intValue’ like that:

	if player.DS.Levels.Value == nil then
		changeDatas.setValue(player.DS.Levels, 1)
	end

now i have another problem, here is the error, if anybody can help that will help me:

1 Like

What does it do? changeDatas.removeValue()

Can you please say at which line the error is happening?
This happens when an event is in an infinite loop.

changeDatas.removeValue() remove the value by 1

This can be a problem, since it changes it and the Changed event is called again and so many times

local TweenService = game:GetService("TweenService")
local ConfigurationXP = require(game:GetService("ReplicatedStorage").Configuration.XP)
local changeDatas = require(game:GetService("ReplicatedStorage").Modules.ChangeDataValue)
local player = game.Players.LocalPlayer
local debounce=false

local function calculBar()
	if debounce then debounce=false; return end
	if player.DS.Levels.Value == nil then
		player.DS.Levels.Value = 0
	end

	if player.DS.Levels.Value then
		if player.DS.EXP.Value >= ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)] then
			debounce=true
			changeDatas.removeValue(player.DS.EXP, ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)])
			changeDatas.addValue(player.DS.Levels, 1)

			local calcul = ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)] - player.DS.EXP.Value
			TweenService:Create(script.Parent.Level.Bar, TweenInfo.new(0.5), {Size = UDim2.new(1 / calcul, 0, 1, 0)}):Play()
			script.Parent.Level.Label.Text = "LEVEL " .. tostring(player.DS.Levels.Value) .. " · " .. tostring(player.DS.EXP.Value) .. " EXP"

		end
	end
end

calculBar()
player.DS.EXP:GetPropertyChangedSignal('Value'):Connect(calculBar)

here is all my output, the problem is that the value don’t change but it’s making a loop

Maybe make a debounce variable that checks if it has already been changed and if not continue

@Pcoi94 check the script and tell me if it works

thhe script is working fine, but the script is looping, this is the problem

again? show script for changeDatas.removeValue()

local module = {}

function module.addValue(path, number:number) path.Value += number; end
function module.removeValue(path, number:number) path.Value -= number; end
function module.setValue(path, number:number) path.Value = number; end

return module
local TweenService = game:GetService("TweenService")
local ConfigurationXP = require(game:GetService("ReplicatedStorage").Configuration.XP)
local changeDatas = require(game:GetService("ReplicatedStorage").Modules.ChangeDataValue)
local player = game.Players.LocalPlayer

local function calculBar()
	if player.DS.Levels.Value ~= nil then
		changeDatas.setValue(player.DS.Levels, 1)
	end
	
	if player.DS.Levels.Value then
		if player.DS.EXP.Value >= ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)] then
			changeDatas.removeValue(player.DS.EXP, ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)])
			changeDatas.addValue(player.DS.Levels, 1)
			
			local calcul = ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)] - player.DS.EXP.Value
			TweenService:Create(script.Parent.Level.Bar, TweenInfo.new(0.5), {Size = UDim2.new(1 / calcul, 0, 1, 0)}):Play()
			script.Parent.Level.Label.Text = "LEVEL " .. tostring(player.DS.Levels.Value) .. " · " .. tostring(player.DS.EXP.Value) .. " EXP"

		end
	end
end

task.wait(1)
calculBar()
player.DS.EXP:GetPropertyChangedSignal('Value'):Connect(calculBar)

local buttons = script.Parent.Towers

local function ImageSize(path, Time, udim2)
	TweenService:Create(path, TweenInfo.new(Time), {Size = udim2}):Play()
end

local function clickAnimation(path)
	local clone = script.ClickAnimation:Clone()
	clone.Parent = path
	
	local tween = TweenService:Create(clone, TweenInfo.new(0.2), {Size = UDim2.new(1.2, 0, 1.2, 0)}); tween:Play()
	tween.Completed:Connect(function()
		local tween2 = TweenService:Create(clone, TweenInfo.new(0.085), {BackgroundTransparency = 1}); tween2:Play()
		tween2.Completed:Connect(function() clone:Destroy(); end)
		
	end)
	
end


buttons['1'].Button.MouseEnter:Connect(function() ImageSize(buttons['1'].Image, 0.15, UDim2.new(0.8, 0, 0.8, 0)); end)
buttons['1'].Button.MouseLeave:Connect(function() ImageSize(buttons['1'].Image, 0.15, UDim2.new(0.7, 0, 0.7, 0)); end)
buttons['1'].Button.MouseButton1Down:Connect(function() clickAnimation(buttons['1']); end)

buttons['2'].Button.MouseEnter:Connect(function() ImageSize(buttons['2'].Image, 0.15, UDim2.new(0.8, 0, 0.8, 0)); end)
buttons['2'].Button.MouseLeave:Connect(function() ImageSize(buttons['2'].Image, 0.15, UDim2.new(0.7, 0, 0.7, 0)); end)
buttons['2'].Button.MouseButton1Down:Connect(function() clickAnimation(buttons['2']); end)

buttons['3'].Button.MouseEnter:Connect(function() ImageSize(buttons['3'].Image, 0.15, UDim2.new(0.8, 0, 0.8, 0)); end)
buttons['3'].Button.MouseLeave:Connect(function() ImageSize(buttons['3'].Image, 0.15, UDim2.new(0.7, 0, 0.7, 0)); end)
buttons['3'].Button.MouseButton1Down:Connect(function() clickAnimation(buttons['3']); end)

buttons['4'].Button.MouseEnter:Connect(function() ImageSize(buttons['4'].Image, 0.15, UDim2.new(0.8, 0, 0.8, 0)); end)
buttons['4'].Button.MouseLeave:Connect(function() ImageSize(buttons['4'].Image, 0.15, UDim2.new(0.7, 0, 0.7, 0)); end)
buttons['4'].Button.MouseButton1Down:Connect(function() clickAnimation(buttons['4']); end)

buttons['5'].Button.MouseEnter:Connect(function() ImageSize(buttons['5'].Image, 0.15, UDim2.new(0.8, 0, 0.8, 0)); end)
buttons['5'].Button.MouseLeave:Connect(function() ImageSize(buttons['5'].Image, 0.15, UDim2.new(0.7, 0, 0.7, 0)); end)
buttons['5'].Button.MouseButton1Down:Connect(function() clickAnimation(buttons['5']); end)

are you sure you copied and ran my script?

It all comes down to this line (i think):

player.DS.EXP:GetPropertyChangedSignal('Value'):Connect(calculBar)

To fix it, you need to add a debounce or to disconnect it. I’ll go with the latter for now:

local connection
local function calculBar()
	if player.DS.Levels.Value ~= nil then
		changeDatas.setValue(player.DS.Levels, 1)
	end
	
	if player.DS.Levels.Value then
		if player.DS.EXP.Value >= ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)] then
			changeDatas.removeValue(player.DS.EXP, ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)])
			changeDatas.addValue(player.DS.Levels, 1)
			
			local calcul = ConfigurationXP.xpPerLevels[tonumber(player.DS.Levels.Value)] - player.DS.EXP.Value
			TweenService:Create(script.Parent.Level.Bar, TweenInfo.new(0.5), {Size = UDim2.new(1 / calcul, 0, 1, 0)}):Play()
			script.Parent.Level.Label.Text = "LEVEL " .. tostring(player.DS.Levels.Value) .. " · " .. tostring(player.DS.EXP.Value) .. " EXP"

		end
	end
    connection:Disconnect()
end

connection = player.DS.EXP:GetPropertyChangedSignal('Value'):Connect(calculBar)

Give this a run and see if it works.

In this case, the connection will work only 1 time and then turn off.

Ah, didn’t read the context. disconnecting this connection is definitely not what you want. But I think I know why this is occuring.
In the calculBar() function, you call the .removeValue method for player.DS.EXP. You are subtracting a certain amount from this number.
However, you are also checking to see if this number has been changed. This creates what’s known as a recursive loop in programming. Sometimes, recursive loops are useful (for example, creating a factorial function), but they are generally not good. The reason why you got that error is because the event was being fired far too many times in too short a timespan, because it was recursive.
To fix it, remove the call and place it elsewhere.


the bar don’t even load now…
could you help me place somewhere else that can work fine?