Type 'number?' could not be converted into 'number'

Hello, I have a script after clicking it gives money (the value of money is a string). I use tostring(tonumber(Money) + any number) but after running the beta Roblox studio I get a warning

[image]
image

	if Purchase:FindFirstChild("Cashy") ~= nil then
		if Purchase:FindFirstChild("LEGO") ~= nil then
			CashyImage.Visible = true
			--ADD
			Money.Value = tostring(tonumber(Money.Value) + (math.round(2 * BoostButton.Value * LEGOBoost)))	
		
		else
		CashyImage.Visible = true
		--ADD
		Money.Value = tostring(tonumber(Money.Value) + (math.round(2 * BoostButton.Value)))
	end
end

How to adapt to this?
image

2 Likes

You could use the :: operator to solve this conflict:

tostring(tonumber(Money.Value)::number + (math.round(2 * BoostButton.Value * LEGOBoost)))
tostring(tonumber(Money.Value)::number + (math.round(2 * BoostButton.Value)))
3 Likes

The point of strict is to catch errors before they can even happen.

In your case what happens when tonumber() fails? Because it can’t convert Money.Value to a number and returns nil instead? Then you will get an attempt to perform arithmetic (add) on nil and number error.

Instead you should check whether tonumber() fails, and handle the error if it does.

But looking at your code you should probably change your Money to be a NumberValue, instead of what I assume to be a StringValue. That would make more sense.

3 Likes

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