I've been coding for a bit, and it seems this always confuses me. I couldn't find any good tutorials for what I'm specifically looking for.
How do you get a text value from a gui with remote events?
If you press enter on a gui after you typed something in, I want to know what script I could use in order for that text to appear, say, at the top of the gui.
This question isn't referring to anything specific, I just need a basic understanding of how you would use remote events to get text from a TextBox and put it somewhere else.
How do you get a text value from a gui with remote events?
You ask it in a way such that you would want to request the client to return a value, so I suggest you look into RemoteFunction
's.
https://developer.roblox.com/en-us/api-reference/class/RemoteFunction
A RemoteFunction
is essentially a RemoteEvent
that will return a value when the client (or server) responds with one.
You would want to setup a system such that you could reference a GUI member, and return the text of that member from the remote event.
In this scenario I am going to assume the GUI the player owns came from StarterGui
and therefore was cloned on the server to the player's GUI.
This means the server can reference the GUI objects, but due to how replication works will not see changes in most properties.
So the server could pass reference to the GUI object and the client would index the given GUI object and return the value of the Text
property.
Text
from a specific value from a client you will not need to pass a reference, just store a dedicated reference on the client and use that reference. In code it could be something like this:
We pass a reference
from the server.
--// Server local remoteFunction = ... --// reference RemoteFunction remoteFunction:InvokeClient(someClient, reference)
Client will use the passed reference
variable.
--// Client local remoteFunction = ... --// reference RemoteFunction local function handleInvoke(reference) return reference.Text end remoteFunction.OnClientInvoke = handleInvoke
Server just invokes the client to get the Text
of a dedicated reference
.
--// Server local remoteFunction = ... --// reference RemoteFunction remoteFunction:InvokeClient(someClient)
Client uses their dedicated reference
variable to return the Text
the server wants.
--// Client local remoteFunction = ... --// reference RemoteFunction local reference = ... --// reference to UI object local function handleInvoke() return reference.Text end remoteFunction.OnClientInvoke = handleInvoke
If you would like the client to send the text to the server and the server preforms some action based off that text I would prefer using a RemoteEvent
.
All you would need to do is just send the value of the Text
property of your TextLabel
or TextBox
through the :FireServer
method of the remote event.
RemoteEvent:FireServer(TextBox.Text)