How can I make an NPC say a message I enter in a GUI Text box?

Hi there,

  1. I want to make an NPC say a message that I write in a GUI Text box.

  2. The problem is that when I enter the game, it throws an error saying. Players.manfacemanfrend.PlayerGui.Control.Message.LocalScript:9: attempt to index string with 'Text'

  3. I’ve look everywhere for solutions but there is no category which is even remotely related to this issue.?

The Send Message Button scripts.
The 1st script.

local ChatService = game:GetService("Chat")
local Main = game.Workspace.manfacemanfrend
local Humanoid = Main:FindFirstChildOfClass("Humanoid")
local chatbot = Main
local msg = script.Parent.Parent.Message.Text
local input = msg.Text

function control() 
	ChatService:Chat(chatbot.Head, msg, ".")
	print("government mind control go brrrr")
end

My second Send Button script

local control = script.Parent.controled

script.Parent.MouseButton1Click:Connect(function()
	control:FireServer()
	print("brain wash moment")
end)

The Text Input box script.

local ChatService = game:GetService("Chat")
local Main = game.Workspace.manfacemanfrend
local Humanoid = Main:FindFirstChildOfClass("Humanoid")
local chatbot = Main
local msg = script.Parent.Parent.Message.Text
local input = msg.Text

do
	msg.Text = input
end

The handler script

local Players = game:GetService("Players")
local Main = game.Workspace.Security
local Humanoid = Main:FindFirstChildOfClass("Humanoid")
local chatbot = game.Workspace.Security
local messagebox = script.Parent.Message
local send = script.Parent.Send
local sus = script.Parent.sus
local SayMessage = messagebox.Text
local cs = game:GetService("Chat")

function SayMessage:Chat(Text)
	messagebox.Text = tostring(Text)

	if Text == "" then

		sus.Value = false

	else

		sus.Value = true

		messagebox.Text = Text
		
		cs:Chat(chatbot.Head, "[Controlled]:", Text, Enum.ChatColor.White)
	end

end

Can someone explain to me where I went wrong and how I can fix it please?

Thanks all.

3 Likes

You’re calling .Text on a string value.

local msg = script.Parent.Parent.Message.Text
local input = msg.Text

^^ Note how you declared msg as Message.Text already.

1 Like

Which script did I make the error in?

Your first script.

Additionally you might want to try and compress code into fewer scripts or split different parts of code into their own modules to keep everything neat. Even if it takes longer to write, you can’t put a price on readability.

This also might be a helpful read:
https://roblox.github.io/lua-style-guide/

1 Like

Ok so like making it like this for example

local bot = "bot"
print(bot)

So basically what your saying is I should make more variables to shorten the code?

No I just mean that you having a script for each button, different scripts for handling, etc. can quickly get confusing once you add more code. Organization is key.

1 Like

Alright, so how can I fix this error to make the code work?

Change your first script to

local ChatService = game:GetService("Chat")
local Main = game.Workspace.manfacemanfrend
local Humanoid = Main:FindFirstChildOfClass("Humanoid")
local chatbot = Main
local msg = script.Parent.Parent.Message.Text


function control() 
	ChatService:Chat(chatbot.Head, msg, ".")
	print("government mind control go brrrr")
end

Still getting the exact same error, I think the code you sent was for the send message button

Players.manfacemanfrend.PlayerGui.Control.Message.LocalScript:9: attempt to index string with 'Text'

Change the Text Input box script to

local ChatService = game:GetService("Chat")
local Main = game.Workspace.manfacemanfrend
local Humanoid = Main:FindFirstChildOfClass("Humanoid")
local chatbot = Main
local msg = script.Parent.Parent.Message.Text
local input = msg

do
	msg.Text = input
end

Also, please look at how the problem is being fixed, rather than just having me change it for you.

You’re calling .Text on the msg variable.

The msg variable is :

local msg = script.Parent.Parent.Message.Text

And you’re calling msg.Text, which is the same as calling .Text.Text

This is the exact code I had in the first place

No it’s not, you had local input = msg.Text which is where the error is stemming from, you can’t call .Text on a string value.

Oh I see, I thought by putting Text at the end would be the value of what the person put in

Oh I see now my bad sorry i didnt see

That didn’t work either, do I do input.Value instead of msg.Text?

To add on to the support you’ve already got, make sure you filter the message if this will be in a game and other players can use it. If you don’t your game may end up flagged for review or deleted.

If it’s just a small private project, don’t worry about it and continue on.

1 Like

Can you disclose any error messages? What exactly isn’t working?

Ok I’m not too sure how to filter the chat.

This is the error i’m getting here.

Players.manfacemanfrend.PlayerGui.Control.Message.LocalScript:9: attempt to index string with 'Text'

The same one keeps popping up after I try to send the message I entered in the gui.

Can you paste the script which contains the error?