Attempt to compare nil <= number

I was doing a statistics system and I got this error, does anyone know how to solve it?

Code : (ServerScript)

game.ReplicatedStorage.Remotes.AddPoints.OnServerEvent:Connect(function(player,value,amount)
	local data = player:WaitForChild("Stats")
	local points = data:WaitForChild("Points")
	local num = tonumber(amount)
	if num <= points.Value then
		data:FindFirstChild(value).Value = data:FindFirstChild(value).Value + amount
		points.Value = points.Value - amount
	end
end)	

LocalScript :

for i, Frame in pairs(StatsFrame.Stats:GetChildren()) do
	if Frame:IsA("Frame") then
		for i, Button in pairs(Frame:GetChildren()) do
			if Button:IsA("TextButton") then
				Button.MouseButton1Click:Connect(function()
					remoteevent:FireServer(Frame.Name,Points.Text)
				end)
			end
		end
	end
end

where is the amount variable being assigned

Whatever Points.Text points to is causing your problem.

Try printing our the value of “amount” within the server sided script. If the text has anything other than just digits tonumber will result in nil.

The “tonumber()” global function returns nil if the string value passed as an argument to it cannot be coerced into a number type value, you can handle this behavior by adding a check to ensure that “tonumber()” returns a valid number value.

game.ReplicatedStorage.Remotes.AddPoints.OnServerEvent:Connect(function(player,value,amount)
	local data = player:WaitForChild("Stats")
	local points = data:WaitForChild("Points")
	local num = tonumber(amount)
	if num then --ensure num is a valid number value and not nil
		if num <= points.Value then
			data:FindFirstChild(value).Value = data:FindFirstChild(value).Value + amount
			points.Value = points.Value - amount
		end
	end
end)
1 Like

There seems that amount was not given in the LocalScript and therefore nil. Perhaps place a third parameter that represents amount in RemoteEvent:FireServer()?