Problem with loop in modulescript

Hello everyone,

Since I dont use Lua I get into these problems alot and I am looking for some help. I am making this dialog system where a list of string is given as parameter and i want to loop throught the list and the string but I get the error "attempt to get length of a number value ".

I would appriciate it if someone helped me with this problem

function Dialog.Type(TextList: "list of strings", Speed: "number", Pause: "number")
	local Gui = Dialog.Get_GUI()
	local TextLabel = Gui:FindFirstChild("TextBox")
	for Text = 1, #TextList do
		for Letter = 1, #Text, 1 do
			TextLabel.Text = string.sub(Text,1,Letter)
			wait(Speed)
		end
		if Pause then
			wait(Pause)
		end
	end
end

return Dialog

Thanks for helping!

Are you passing a table of strings? what do you mean by a list?

1 Like

sorry i ment a table instead of a list

1 Like

You were trying to get the length of Text, which on your first for loop is a number.

Try this:

function Dialog.Type(TextList: "list of strings", Speed: "number", Pause: "number")
	local Gui = Dialog.Get_GUI()
	local TextLabel = Gui:FindFirstChild("TextBox")
	for Text = 1, #TextList do
		for Letter = 1, string.len(TextList[Text]), 1 do
			TextLabel.Text = string.sub(TextList[Text],1,Letter)
			wait(Speed)
		end
		if Pause then
			wait(Pause)
		end
	end
end

return Dialog
1 Like

If TextList is an array then you are using #TextList correctly on the first for loop, but for the second for loop, why are you trying to get the length of #Text which is a number value of the for loop?

Im assuming the error happens here?

The thing is, the Text is the index, not the value, so your looping the table with an index not a value, so you would have to change it to use I,v in pairs() or find the value using the index, heres an example:

function Dialog.Type(TextList, Speed, Pause)
    local Gui = Dialog.Get_GUI()
    local TextLabel = Gui:FindFirstChild("TextBox")
    
    for i = 1, #TextList do
        local Text = TextList[i]
        for Letter = 1, #Text do
            TextLabel.Text = string.sub(Text, 1, Letter)
            wait(Speed)
        end
        
        if Pause then
            wait(Pause)
        end
    end
    
    return Dialog
end
2 Likes

This made the error go away but instead it prints the index and not the string. But thanks

I am used to using python and this was the first time that came to me when looking up for a tutorial. But i see where you are coming from and thanks for that.

2 Likes

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