Textbox to Textlabel system not working

I need help making this TextBox system work, please. So I have a TextBox, TextButton, and TextLabel. I am trying to make it so that when the player types in the textbox and presses the button that text will appear on the text label for everyone and have a typewriter sort of effect. I tried connecting it to a Remote Event but that did not help. Here is the LocalScript inside of the frame with the gui’s

local TextBox = script.Parent.TextBox
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("SendMsg")
--local Text = script.Parent:WaitForChild("TextBox").Text
local TextLabel = script.Parent:WaitForChild("TextLabel")
local text = TextBox.Text
local sendmsg = game.ReplicatedStorage:WaitForChild("SendMessageToServer")

TextBox.FocusLost:Connect(function(enterPressed)
	if not enterPressed then
		return
	end
		sendmsg:FireServer()
	end)

Inside ServerScriptService to handle the remote event:

local SendMessageToServer = Instance.new("RemoteEvent")
SendMessageToServer.Name = "SendMessageToServer"
SendMessageToServer.Parent = game.ReplicatedStorage
local TypeSound = game.StarterGui.ScreenGui.Frame5.TextLabel

local TextBox = game.StarterGui.ScreenGui.Frame5.TextBox
--local Text = script.Parent:WaitForChild("TextBox").Text
local TextLabel = game.StarterGui.ScreenGui.Frame5.TextLabel
local text = TextBox.Text


local function sendmsg()
	for i = 1, #text, 1 do
		TextLabel.Text = string.sub(text, 1, i)
		wait(0.05)
		TypeSound:Play()
	end
end

SendMessageToServer.OnServerEvent:Connect(sendmsg)

I do not know how to make this work. Could someone help please? I have tried looking for other solutions but I don’t think I could find any related to my problem. Any help or guidence appreciated.

2 Likes

I am having trouble making the code blocks in my post so excuse my mistake if it looks wonky this is my first post

Could you please format your code?
Also, you need to pass the text as a parameter through the remote event. Then, you need to filter it.

How should I write it to send the text to the remote even parameter as you said?

The text is not being sent from the client to the server using the event. You need to pass in the text variable to the Remote Event.

sendmsg:FireServer(text)

Then, in the function associated with the Remote Event, you can use text as a parameter.

local function sendmsg(player, text)

I recommend that you review the documentation for Remote Events / Functions, this may be helpful.
Remote Functions and Events (roblox.com)

I am trying to write different stuff now to see if I can fix what I thought was the problem and get the error “attempt to get length of a Instance value”. Here is the updated code
Server Script

local SendMessageToServer = Instance.new("RemoteEvent")
SendMessageToServer.Name = "SendMessageToServer"
SendMessageToServer.Parent = game.ReplicatedStorage
local TypeSound = game.StarterGui.ScreenGui.Frame5.TextLabel

local function sendmsg(object,text,length)
	for i = 1,#text,1 do
		object.Text = string.sub(text,1,i)
			wait(0.05)
		TypeSound:Play()
	end
end
SendMessageToServer.OnServerEvent:Connect(function(player, Textbox)
	sendmsg(Textbox, Textbox.Text, 0.05)
	end)

SendMessageToServer.OnServerEvent:Connect(sendmsg)

Local Script

local TextBox = script.Parent.TextBox
local Textbox = TextBox
local sendmsg = game.ReplicatedStorage:WaitForChild("SendMessageToServer")

script.Parent.TextButton.MouseButton1Click:Connect(function()
 	sendmsg:FireServer(Textbox)
	end)

You should restructure your code. Try this:

Local Script

local TextBox = script.Parent.TextBox
local sendmsg = game.ReplicatedStorage:WaitForChild("SendMessageToServer")

script.Parent.TextButton.MouseButton1Click:Connect(function()
 	sendmsg:FireServer(TextBox, 0.05)
end)

Server Script

local SendMessageToServer = Instance.new("RemoteEvent")
SendMessageToServer.Name = "SendMessageToServer"
SendMessageToServer.Parent = game.ReplicatedStorage

local TypeSound = game.StarterGui.ScreenGui.Frame5.TextLabel

SendMessageToServer.OnServerEvent:Connect(function(player, TextBox, interval_length)
    -- Get the initial Text value from the TextBox.
    text = TextBox.Text

    -- Iterate through each letter in the Text string.
	for index, letter in ipairs(text)
		TextBox.Text = string.sub(text, 1, index)
		wait(interval_length)
		TypeSound:Play()
	end
end)

See if this returns a different error.

I had to try to patch up the code you gave me since it gave a few errors in the lines. But I tried to make it work. The server script script

local SendMessageToServer = Instance.new("RemoteEvent")
SendMessageToServer.Name = "SendMessageToServer"
SendMessageToServer.Parent = game.ReplicatedStorage
local TextBox = game.StarterGui.ScreenGui.Frame5.TextBox
local TypeSound = game.StarterGui.ScreenGui.Frame5.TextLabel.TypeSound

SendMessageToServer.OnServerEvent:Connect(function(player, TextBox, interval_length)
	-- Get the initial Text value from the TextBox.
local	text = TextBox.Text

	-- Iterate through each letter in the Text string.
	for index, letter in ipairs(text) do
		TextBox.Text = string.sub(text, 1, index)
		wait(interval_length)
		TypeSound:Play()
	end
end)

When I playtested and pressed the button it returned " ServerScriptService.GetMsg:12: invalid argument #1 to ‘ipairs’ (table expected, got string)" And when I tried your original script it just returned “infinite yield possible on ‘ReplicatedStorage:WaitForChild(“SendMessageToServer”)’”

Sorry, I think I made a mistake with my original code. Since you are trying to iterate through a string, you can change

for index, letter in ipairs(text) do

to

for index, letter in ipairs(text:split("")) do

This will give you a Table of chars to iterate through.

Okay, I changed that line and when I pressed the button it didn’t show any error but also didn’t change the text label. And on line 9 I don’t know if I’m supposed to put a local there or not but without local behind it it just has an error underneath “text”.

In your Server script:

You cannot set the value of TextBox from the Server script this way because this is handled on the Client side. Remote Events can be used for one-way communication from the Server to the Client.

RemoteEvent Documentation

Since this script is on the Server side and the TextBox is on the client side, you will need to:

  1. Write a function in the TextBox’s LocalScript to set TextBox.Text to any given value.
  2. Create a Remote Event that can be fired in the Server script.
  3. Connect the Remote Event in your LocalScript with the function that updates the TextBox.Text.
  4. Fire the remote event and pass in the value of string.sub(text, 1, index) rather than setting TextBox.Text directly in your Server script.

Is the code trying to edit the Textbox’s text to whatever we input in the textbox? Where is that Text Box input going into

You are passing an Instance of TextBox to the Server side, but once you pass this Instance to the Server side, it is essentially a new object, meaning that it is not the same Instance that you are trying to update on the client side. If you fire a Remote Event from the Client to the server, you can update the TextBox on the client side using your LocalScript.

I don’t think I explained it well enough but I am trying to grab the text input from the textbox and put it in the Text label and replicate it to all other clients. Is this what the script is supposed to do it looks like it’s trying to change the text box’s text. Thanks for helping.

Yes, you will need to use a Remote Event and connect it with a function that will update the Text Label’s text in a Local Script. You will need to communicate actions from the Server to the Client. I think I phrased my response incorrectly, but the overall concept is the same.

You should be using utf8.graphemes (I believe that’s what it’s called) for language support and better performance.

Server to the client? Or the client to server back to the client? Since I need to grab the client inputted text

You need to send the user’s input text to the server so that it can replicate the text across all clients.

Roblox Client-Server Model