Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fix the attempt to compare boolean with number error roblox?

The error is Players.Player1.PlayerGui.EggSellGui.MainFrame.ShopFrame.BuggyEgg.LocalScript:29: attempt to compare boolean with number

the error is: if player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value == player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value <= 30 then

My script is:

local button = script.Parent
local cost = 30
local egg = script.Parent.Parent.Parent:WaitForChild("EggImage")
local choice1 = script.Parent.Parent.Parent:WaitForChild("Choice1")
local choice2 = script.Parent.Parent.Parent:WaitForChild("Choice2")
local choice3 = script.Parent.Parent.Parent:WaitForChild("Choice3")
local costlabel = script.Parent.Parent.Parent:WaitForChild("Cost")
local player = game.Players.LocalPlayer
local hatchingmoment = script.Parent.Parent.Parent.Parent:WaitForChild("HatchingMoment")
local hatchedimage = script.Parent.Parent.Parent.Parent.HatchingMoment:WaitForChild("HatchedImage")

button.MouseEnter:connect(function()
	egg.Image = "http://www.roblox.com/asset/?id=6811340"
	choice1.Image = "http://www.roblox.com/asset/?id=711466052"	
	choice2.Image = "http://www.roblox.com/asset/?id=711466415"
	choice3.Image = "http://www.roblox.com/asset/?id=142257783"
	costlabel.Text = "Cost: 30 Grass Element Tokens"
end)

button.MouseLeave:connect(function()
	egg.Image = ""
	choice1.Image = ""	
	choice2.Image = ""
	choice3.Image = ""
	costlabel.Text = ""
end)

button.MouseButton1Down:connect(function()
	if player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value == player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value <= 30 then
		player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value = player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value - cost
		local randomlychoose = math.random(1,20)
		if randomlychoose == 1 or randomlychoose == 2 or randomlychoose == 3 or randomlychoose == 4 or randomlychoose == 5 or randomlychoose == 6 or randomlychoose == 7 or randomlychoose == 8 or randomlychoose == 9 or randomlychoose == 10 or randomlychoose == 11 or randomlychoose == 12 or randomlychoose == 13 or randomlychoose == 14 or randomlychoose == 15 then
			hatchingmoment.Visible = true
			wait(10)
			hatchedimage.Image = "http://www.roblox.com/asset/?id=711466052"
			if randomlychoose == 16 or randomlychoose == 17 or randomlychoose == 18 or randomlychoose == 19 then
				hatchingmoment.Visible = true
				wait(10)
				hatchedimage.Image = "http://www.roblox.com/asset/?id=711466415"
			end
		end		
	end
end)

error is on if player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value == player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value <= 30 then

Please help fix

2 comments

0

Edited to fix code formatting

0

The error is self-explanatory you are using <= with a boolean value ie true <= which is not valid.

1 answer

✓ Accepted answer

Well you are comparing a boolean with a number. num == num returns a boolean as well as num <= num. and you have it as num == num -> False <= num (which throws the error when comparing the boolean with a number).

A simple fix is to add and in the if statement, so it should be like this:

if player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value == player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value and player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value <= 30 then

Also, comparing a value with itself will always return true and a little suggestion, make player:WaitForChild("leaderstats"):WaitForChild("GrassTokens").Value a local variable since you use it several times.

This discussion is preserved as it was. New answers, comments, and votes are closed.