Detecting text in textbox Error

I am trying to make a math game where if you answer it correctly you will get cash, but it doesn’t work. here is the script:

local GamePassID = 37494039
local two = false
local MarketService = game:GetService("MarketplaceService")

local function CheckForGamepass(Plr)
	local PlayerHasGamePass = MarketService:UserOwnsGamePassAsync(Plr.UserId, GamePassID)
	if PlayerHasGamePass == true then
		two = true
	elseif PlayerHasGamePass == false then
		two = false
	end
end

game.Players.PlayerAdded:Connect(CheckForGamepass)
local num1 = math.random(1,50)
	local num2 = math.random(1,50)
	local ans = num1 + num2
	script.Parent.Text = "What is: " ..num1 .."+" ..num2
	script.Parent.Parent.TextBox.Changed:Connect(function()
		if script.Parent.Parent.TextBox == tostring(ans) then
			print("correct")
			script.Parent.Parent.Parent.Visible = false
			if two == true then
				game.ReplicatedStorage.Give2Cash:FireServer()
			else
			game.ReplicatedStorage.GiveCash:FireServer()
			end
		
		end

	end)
	

please help if u can

you forgot the .Text

if script.Parent.Parent.TextBox.Text == tostring(ans) then
1 Like

It still wont work

Try using Textbox.FocusLost

script.Parent.Parent.TextBox.FocusLost:Connect(function(enter)
	if enter and script.Parent.Parent.TextBox == tostring(ans) then
		print("correct")
		script.Parent.Parent.Parent.Visible = false
		if two == true then
			game.ReplicatedStorage.Give2Cash:FireServer()
		else
			game.ReplicatedStorage.GiveCash:FireServer()
		end
	end
end)

It still wont work, nothing happens after I did that.

are there any errors?
if not, try to add prints all along the script and see when it stops printing.

Can I see the explorer, where the TextBox is parented and the TextLabel

image
the explorer

no, there are no errors on the script

Hopefully I understood your parenting properly. Can you replace this

with this

script.Parent.Parent.Visible = false

Shouldn’t you be turning the .Visible of the parent of TextLabel to false


still wont work

Try doing this instead:

local GamePassID = 37494039
local two = false
local MarketService = game:GetService("MarketplaceService")

local function CheckForGamepass(Plr)
	local PlayerHasGamePass = MarketService:UserOwnsGamePassAsync(Plr.UserId, GamePassID)
	if PlayerHasGamePass == true then
		two = true
	elseif PlayerHasGamePass == false then
		two = false
	end
end

function onFocusLost(answered, ans)
    if tonumber(answered) == ans then
        print("correct")
	script.Parent.Parent.Parent.Visible = false
	if two == true then
		game.ReplicatedStorage.Give2Cash:FireServer()
	else
		game.ReplicatedStorage.GiveCash:FireServer()
	end
    end
end

game.Players.PlayerAdded:Connect(CheckForGamepass)
    local num1 = math.random(1,50)
    local num2 = math.random(1,50)
    local ans = num1 + num2
    script.Parent.Text = "What is: " ..num1 .." + " ..num2
    script.Parent.Parent.TextBox.FocusLost:Connect(function(enterpressed,input)
	onFocusLost(script.Parent.Parent.TextBox.Text, ans)
    end)
end
local GamePassID = 37494039
local two = false
local MarketService = game:GetService("MarketplaceService")

local function CheckForGamepass(Plr)
	local PlayerHasGamePass = MarketService:UserOwnsGamePassAsync(Plr.UserId, GamePassID)
	if PlayerHasGamePass == true then
		two = true
	elseif PlayerHasGamePass == false then
		two = false
	end
end

game.Players.PlayerAdded:Connect(function(Player)
CheckForGamepass(Player)
local num1 = math.random(1,50)
	local num2 = math.random(1,50)
	local ans = num1 + num2
	script.Parent.Text = "What is: " ..num1 .."+" ..num2
	script.Parent.Parent.TextBox.FocusLost:Connect(function()
		if script.Parent.Parent.TextBox.Text == tostring(ans) then
			print("correct")
			script.Parent.Parent.Parent.Visible = false
			if two then
				game.ReplicatedStorage.Give2Cash:FireServer()
			else
			game.ReplicatedStorage.GiveCash:FireServer()
			end
		
		end

	end)

:smile: