Cannot assign value string expected error

image
I find that the script fails to work and I cannot find the source to where it is failing to place a string the textlabel. Here is the script:

local plrs = game:GetService('Players')
local lp = plrs.LocalPlayer
local ChatBox = script.Parent
local replicatedstorage = game:GetService('ReplicatedStorage')
local ManageFrame = script.Parent.Parent
local msgcloner = script.Parent.Parent.MsgCloner

Greetings = {
	"Hello",
	"Hi",
	"hello",
	"hi",
	"Hola",
	"hola",
	"yo",
	"Yo",
	"Hi!",
	"hi",
	"Howdy",
	"howdy",
}

local function createchat(origin, text)
	for i,v in pairs(msgcloner:GetChildren()) do
		local msgpieces = v:Clone()
		local msgbase = msgcloner:Clone()
		wait()
		msgbase.Parent = ManageFrame
		msgpieces.Parent = msgbase
		msgbase.BackgroundTransparency = 0.2
		msgbase.TextTransparency = 0
		if origin == "ai" then
			msgbase.Position = UDim2.new(0.028, 0.692, 0)
			msgbase.Text = text
			ChatBox.Text = ""
		else
			msgbase.Position = UDim2.new(0.653, 0, 0.692, 0)
			msgbase.Text = text
			ChatBox.Text = ""
		end
	end
end

local function searchlistai(text)
	print('plss')
	for i, v in pairs(Greetings) do
		if string.find(v, text) then
			createchat('ai', v[math.random(1,11)])
		end
	end
end

local function onFocusLost(enterPressed)
	if enterPressed then
		createchat('plr', ChatBox.Text)
		searchlistai(ChatBox.Text)
	end
end

ChatBox.FocusLost:Connect(onFocusLost)

The print placed where I should be searching for a value in table fails. Meaning the function wont even load. What is going on here?

On line 40, you are calling the function “createchat” and passing a wrong value in the second argument.
here:

for i, v in pairs(Greetings) do -- Creates a loop, so it will run 12 times, because there are 12 objects in Greetings table, and each time, "v" variable is going to be one of the values from the table (a string)
	if string.find(v, text) then
		createchat('ai', v[math.random(1,11)]) -- Here, you are trying to access a table using the "v" value, which is already a string from the table
	end
end

So if you want to pass a random string, you should do it like this:

createchat('ai', Greetings[math.random(1,#Greetings)])

However if you want to use your function, it should work like this:

local function searchlistai(text)
	for i, v in pairs(Greetings) do
		if string.find(v, text) then
			createchat('ai', v)
		end
	end
end
1 Like

Thanks a bunch! However line 36 (msgbase.Text = text) still fails? I have no idea what to do here since it should be sent properlly.

Edit: I used the wrong script even though I read through your code. Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.