Transferring Text with RemoteEvent

I’m attempting to get user’s text from the TextBox when clicked on the submit button, transferring the data to print to the output - but unfortunately, it doesn’t function for some reason as intended. What’s the concurrent issue here? Here’s the code:

-- This is in the localscript
local rep = game.ReplicatedStorage.SetIcon
local txt = script.Parent.Parent.TextBox.Text

script.Parent.MouseButton1Click:Connect(function()
	rep:FireServer(txt)
end)
-- This is in the script
local rep = game.ReplicatedStorage.SetIcon
rep.OnServerEvent:Connect(function(plr,	txt)
	print("User has typed "..txt.." in the textbox!")
end)

image image

image
This is the outcome, but it left my phrase blank for an unknown reason.

Change txt to
script.Parent.Parent.TextBox.Text
As you’re trying to send the saved variable, the variable will not change after set, unless you tell it to.
You can move txt into the function, and then it will update. However, with it being outside the funciton it will not update.

So in this case the right code is: [on localscript]

script.Parent.MouseButton1Click:Connect(function()
	rep:FireServer(script.Parent.Parent.TextBox.Text)
end)
3 Likes