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

Why doesn't the studio recognize this as equal?

No one on the Roblox forum has answered this and I can't continue working on my place until it's figured out.

For some odd reason the studio doesn't read two of my variables, which have identical values, as equal. What happens is a player clicks on a brick, and a simple addition problem appears in their player gui. They can type in the answer on the gui and check the answer with a button.

function onClicked(p)

	script.Parent.Number1.Value = math.random(1,5)
	script.Parent.Number2.Value = math.random(1,5)


	local var1 = script.Parent.Number1.Value
	local var2 = script.Parent.Number2.Value

	wait(1) --Needs to wait so other script can check for current Gui's and destroy them

	Instance.new("ScreenGui",p.PlayerGui)
	local g = p.PlayerGui.ScreenGui

	Instance.new("Frame",g)
	local f = g.Frame
	f.Name = "Box"
	f.Size = UDim2.new(0,300,0,50)
	f.Position = UDim2.new(0.5,0,0.5,0)

	Instance.new("TextLabel",g)
	local n1 = g.TextLabel
	n1.Name = "Number1"
	n1.Size = UDim2.new(0,30,0,30)
	n1.Position = UDim2.new(0.52,0,0.52,0)
	n1.FontSize = "Size36"
	n1.Text = var1
	n1.BorderSizePixel = "0"

	Instance.new("TextLabel",g)
	local n2 = g.TextLabel
	n2.Name = "Number2"
	n2.Size = UDim2.new(0,30,0,30)
	n2.Position = UDim2.new(0.52,100,0.52,0)
	n2.FontSize = "Size36"
	n2.Text = var2
	n2.BorderSizePixel = "0"

	Instance.new("TextLabel",g)
	local p = g.TextLabel
	p.Name = "PlusSign"
	p.Size = UDim2.new(0,30,0,30)
	p.Position = UDim2.new(0.52,50,0.52,0)
	p.FontSize = "Size36"
	p.Text = "+"
	p.BorderSizePixel = "0"

	Instance.new("TextLabel",g)
	local e = g.TextLabel
	e.Name = "EqualSign"
	e.Size = UDim2.new(0,30,0,30)
	e.Position = UDim2.new(0.52,150,0.52,0)
	e.FontSize = "Size36"
	e.Text = "="
	e.BorderSizePixel = "0"

	Instance.new("TextBox",g)
	local a = g.TextBox
	a.Name = "Answer"
	a.Size = UDim2.new(0,30,0,30)
	a.Position = UDim2.new(0.52,200,0.52,0)
	a.FontSize = "Size36"
	a.Text = "?"
	a.BorderSizePixel = "0"

	Instance.new("NumberValue",a)
	local value = a.Value
	value.Name = "InputValue"
	value.Value = value.Parent.Text

	Instance.new("TextButton",g)
	local check = g.TextButton
	check.Name = "Check"
	check.Size = UDim2.new(0,300,0,25)
	check.Position = UDim2.new(0.50,0,0.50,50)

	Instance.new("NumberValue",g)
	local needed = g.Value
	needed.Name = "CorrectAnswer"
	needed.Value = var1 + var2

	local answer = game.Workspace.Answer:Clone()
	answer.Parent = needed
	

	print(g)
	print(f)
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

The button has a script which, when clicked, checks the two numbers being added, what the answer should be, and what the answer typed is. All the prints were for finding out where my script was going wrong.

function check()
	local input = script.Parent.Parent.Answer.Text --the answer typed
	local inputvalue = script.Parent.Parent.Answer.InputValue.Value --IntValue
	print("The input is "..input..".")
	print("The value is "..inputvalue..".")
	
	inputvalue = input --IntValue becomes the answer typed
	print("The updated value is "..inputvalue..".")

	local correct = script.Parent.Value --what the answer should be
	print("Current inputvalue is "..inputvalue..".")
	print("Answer should be "..correct..".")

	if inputvalue == correct then --Here is the issue
		print("Correct!")
		print(""..inputvalue.." = "..correct.."")
	else
		print("Incorrect!")
		print(""..inputvalue.." ~= "..correct.."")
	end
end

script.Parent.Parent.Check.MouseButton1Click:connect(check)

For some reason, even when the print command tells me 'inputvalue' and 'correct' are identical numbers, the if function say's their false. Also, if I put:

if inputvalue == input then

as I stated earlier in the script, then the if function works fine. For some reason the script thinks the variable 'correct' isn't identical to 'inputvalue' despite the print command returning the exact number for help.

2 comments

0

Maybe it's because one is a string and one is a numerical value.

0

How so? I pulled both answers from a NumberValue. Would setting inputvalue = input cause the numerical value to turn into a string?

1 answer

✓ Accepted answer

I could be totally wrong, and too dead tired to solve this, but the value of "input" is a string value, you need to change that to a number value so you would use tonumber

so the value would look like

local input= tonumber(script.Parent.Parent.Answer.Text)

2 comments

0

Correct. For == to return true, the left and right have to have the same type.

0

WOOO! Thank you! This is exactly what I needed! This also let's me clean my script up a bunch with this one tool. Thanks! I was wondering if they weren't the same type, but I didn't know how to convert it like this.

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