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

How would I go about reading the correct TextBox.Text from PlayerGui after a TextButton click?

I stumbled across this issue and I am unsure of how to get around it as my overall experience with working around FilteringEnabled is quite low. This is for a single-player game.

Basically, there is a TextBox (which a player is supposed to type something into) and a TextButton (an "Enter" button). When the button is clicked on, it fires a RemoteEvent in game.Workspace. The script inside that RemoteEvent is supposed to print the text of the TextBox. The issue is that it will not detect any changes to the TextBox's text and as a result, only the original text gets printed no matter what.

This is the code of the LocalScript in the TextButton (it works as intended):

script.Parent.MouseButton1Click:connect(function(player)
	game.Workspace.RemoteEvent:FireServer()
end)

This is the code in a Script inside that RemoteEvent:

game.Workspace.RemoteEvent.OnServerEvent:Connect(function()
	local Player = game.Players:FindFirstChildWhichIsA("Player") --my game is single-player, so this works as intended
	local text = Player.PlayerGui.ScreenGui.Frame.TextBox.Text --the TextBox's text
	print(text)
end)

What would be the most simple/best way for this particular RemoteEvent script to read the most recent Textbox.Text? There will be more added to the RemoteEvent Script later on, which will rely on the updated text.

1 answer

✓ Accepted answer

When you're typing something into a TextBox you're typing it from your client so it'll only change on your client, it won't change on the server. So I guess the only way to fix this would to get the TextBox's text on the client and then pass it over to the server with the RemoteEvent.

Local Script

local Player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function() -- The MouseButton1Click event doesn't have any parameters so player will be nil.
	
	local Text = Player.PlayerGui.ScreenGui.Frame.TextBox.Text 

	game.Workspace.RemoteEvent:FireServer(Text)
end)

Server Script

-- OnServerEvent automatically passes the player that fired the RemoteEvent from the client.
game.Workspace.RemoteEvent.OnServerEvent:Connect(function(Player, Text)
	print(Text)
end)

1 comment

0

This worked. Thank you!

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