TextBox.Text value is always nil

Hi! :slight_smile:

Every time I try to get/print the TextBox.Text value in a LocalScript the output is always nil.
Numbers written on the TextBox are supposted to always go thru, only when the player doesn’t write anything or writes anything except a number the output is supposted to be nil.

Here is the LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local inputEvent = ReplicatedStorage:FindFirstChild("Input")
local event = ReplicatedStorage:FindFirstChild("DoGuiTeleport")
local input = tonumber(game.Players.LocalPlayer.PlayerGui.Teleport.Frame.Specific.TextBox.Text)
local path = game.Players.LocalPlayer.PlayerGui.Teleport.Frame.Specific.Info.Destination
local stage = game.Players.LocalPlayer.leaderstats.Stage.Value

script.Parent.MouseButton1Down:Connect(function()
	print(input)
	if input ~= nil then
		if input <= stage then
			inputEvent:FireServer(path, input)
			wait(0.5)
			event:FireServer(path)
			script.Parent.Parent.Parent.Visible = false
		elseif input > stage then
			script.Parent.Parent.TextBox.PlaceholderText = "You can only teleport to stages you've already completed!"
			wait(1)
			script.Parent.Parent.TextBox.PlaceholderText = "Specific Stage"
		end
	else
		script.Parent.Parent.TextBox.PlaceholderText = "Input a valid stage!"
		wait(1)
		script.Parent.Parent.TextBox.PlaceholderText = "Specific Stage"
	end
end)

Can someone help me fix this?
Thanks!

I think the problem is that input isn’t the updated text of the textbox.

2 Likes

You’re defining the input variable immediately at the top of the script, which means it is just going to return whatever is in the TextBox at the start rather than whatever is in it when you actually press the button.

This should work:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local inputEvent = ReplicatedStorage:FindFirstChild("Input")
local event = ReplicatedStorage:FindFirstChild("DoGuiTeleport")
local path = game.Players.LocalPlayer.PlayerGui.Teleport.Frame.Specific.Info.Destination
local stage = game.Players.LocalPlayer.leaderstats.Stage.Value

script.Parent.MouseButton1Down:Connect(function()
	local input = tonumber(game.Players.LocalPlayer.PlayerGui.Teleport.Frame.Specific.TextBox.Text)
	if input ~= nil then
		if input <= stage then
			inputEvent:FireServer(path, input)
			wait(0.5)
			event:FireServer(path)
			script.Parent.Parent.Parent.Visible = false
		elseif input > stage then
			script.Parent.Parent.TextBox.PlaceholderText = "You can only teleport to stages you've already completed!"
			wait(1)
			script.Parent.Parent.TextBox.PlaceholderText = "Specific Stage"
		end
	else
		script.Parent.Parent.TextBox.PlaceholderText = "Input a valid stage!"
		wait(1)
		script.Parent.Parent.TextBox.PlaceholderText = "Specific Stage"
	end
end)
1 Like

as @10kgoldxdrip said code doesn’t get updated text because you only checked once by having only one rbxscriptconnection try using loops

1 Like

That was the problem! Thank You!

Thanks @Developing_Maniac and @Jenny1223im too!
I will mark @10kgoldxdrip comment as Solution because he answered first though.