Addition isn't working?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Hello! I am making a new job for my game and the mini-game I’m making isn’t working. The game is basically where you get two numbers to add together, and if you do it correctly, an indicator will turn green and give you another question. Else, you will get a red signal on the indicator.

Here’s my server script:



local ReplicatedStorage = game:GetService("ReplicatedStorage")


local remoteEvent = ReplicatedStorage.CashierJobRemotes:WaitForChild("StartFromGUI")

-- Create a new part
local function StartJob(player)
	
	
end

-- Call "onCreatePart()" when the client fires the remote event
remoteEvent.OnServerEvent:Connect(StartJob)


--------------------------------------------------------------------------------

--------------------------------------------------------------------------------


-- Equation getter

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteFunction = ReplicatedStorage.CashierJobRemotes:WaitForChild("GetEquation")

-- Create a new part and return it
local function createEquation(player)
	local ans = nil
	local num1 = math.random(1, 15)
	local num2 = math.random(1, 15)
	local sign = "+"
	if sign == "+" then
		ans = num1 + num2
		print(ans)
	end
	print(num1, num2, sign, ans)
	
	
	
	
	return num1, num2, sign, ans
end

-- Bind the "createPart()" function to the remote function's "OnServerInvoke" callback
remoteFunction.OnServerInvoke = createEquation

local script


local part = script.Parent
local answer = nil
-- Save the current state
local currentColor = part.Visible

local function getnum()
	local ReplicatedStorage = game:GetService("ReplicatedStorage")

	local remoteFunction = ReplicatedStorage.CashierJobRemotes:WaitForChild("GetEquation")

	-- Invoke the function
	local num1, num2, sign, ans = remoteFunction:InvokeServer()
	print("The server created the requested part:", num1, num2, sign, ans)

	answer = ans

	local Number = num1 .. "+" .. num2 .. "="
	print(Number)

	script.Parent.EquationDis.equdisplay.Text = Number
end



local function onVischanged()
	local newColor = part.Visible
	print(currentColor, newColor)
	currentColor = newColor
	
	if newColor == true then
		-- Get the first equation
		
		
getnum()


		
		
	end
end 
part:GetPropertyChangedSignal("Visible"):Connect(onVischanged)

--------------------------------------------------------------------------

script.Parent.TextBox.FocusLost:connect(
	function(enterPressed)
		if enterPressed then
			print(script.Parent.TextBox.Text)
			print(answer)
			if script.Parent.TextBox.Text == answer then
				print(answer)
				
				print("Right")
				script.Parent.CorrectOrNot.BackgroundColor3 = Color3.new(0.0980392, 1, 0) 
				wait(.2)
				script.Parent.CorrectOrNot.BackgroundColor3 = Color3.new(0.278431, 0.278431, 0.278431)
				getnum()
			else 
				print(answer)
				
				print("Wrong")
				script.Parent.CorrectOrNot.BackgroundColor3 = Color3.new(1, 0, 0.0156863) 
				wait(.2)
				script.Parent.CorrectOrNot.BackgroundColor3 = Color3.new(0.278431, 0.278431, 0.278431)
				getnum()
			end
		end
	end
)


I have a feeling this may have something to do with string/number communication but I’m not exactly sure what I’m doing wrong here. (Sorry if this is really simple, this is my first time really using numbers in my scripts, surprising, I know.)

here’s the output by the way:

image

Probably because the answer is a string and you’re trying to compare a string to a number. Use tonumber() to convert the text into an actual number.
So do:
if tonumber(script.Parent.TextBox.Text ) == answer then

3 Likes

I’ll give that a try, sounds like it is the solution!

Edit: yep it worked! Thanks!