A good error? This is strange

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to know why my script works, but throws an error at my face (not literally lol)

  2. What is the issue?
    I get this error every time I run the following script.

Script:

local frame = script.Parent.Parent
local textLabel = frame:WaitForChild("Text")
local characterImage = frame:WaitForChild("CharacterImage")

local characterImageModule = require(game:GetService("ReplicatedStorage").CharacterImages)

function write(Label, message, duration)
	print(Label, message, duration)
	Label.Text = ""
	local split = string.split(message, "")
	for i, v in pairs(split) do
		script.tap:Play()
		Label.Text = Label.Text .. v
		wait(0.085)
	end
	wait(tonumber(duration))
	Label.Text = ""
	script.Parent.Parent.Parent.Enabled = false
end

game:GetService("ReplicatedStorage").Events.Talk.OnClientEvent:Connect(function(message, name)
	print("We are talking to an NPC!")
	local imageId = characterImageModule[name]
	print(imageId)
	if imageId and textLabel.Text == "" then
		characterImage.Image = imageId
		script.Parent.Parent.Parent.Enabled = true
		
		print(typeof(textLabel), typeof(message), typeof(3))
		
		textLabel.Text = write(textLabel, message, tonumber(3)) -- the line that errors
		script.Parent.Parent.Parent.Enabled = false
		textLabel.Text = ""
	end
end)

Error:

  19:15:18.859  Players.Zompocolypse.PlayerGui.NPC-Chatting.Base.Scripts.LocalScript:31: invalid argument #3 (string expected, got nil)  -  Client - LocalScript:31

  1. What solutions have you tried so far?
    The code works, but it gives me said error.

Thanks to any who reply!

1 Like

What is line 31? I don’t see where that is.

1 Like

Oh, sorry, it the the following line (I also added a comment in the script above).

textLabel.Text = write(textLabel, message, tonumber(3))

Put this on the line above and let me know what it says.
print(textLabel, message, tonumber(3))
Also, 3 is already a number and you don’t need to use tonumber on it.

1 Like

Here is what it printed:

Text - I hear there is some nerd kid who collects bugs. You might find him at the boy's cabin. - 3 (I added the dashes so you can clearly see the individual items.)

try this

textLabel.Text = write(textLabel, message, 3)

as im sure you cant change a number into a number

or try tostring() for the 3

I only used the tonumber() function as the error saidinvaild argument #3. So, I thought it was reading the number 3 as nil or something. Plus, using the function will not change a number to a number if its already a number. (I also already tried that)

Can you rename the write function to something else?

Why would I do that? The function creates that typewriter effect when writing text to the given textlabel (which is what I want).

Never mind, it was a shot in the dark because I’m dumb and couldn’t find the problem.
It looks like your problem is that write doesn’t return a string.
If I’m reading it right, the solution is to change the line to this.
write(textLabel, message, 3)

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Variables
local frame = script.Parent.Parent
local textLabel = frame:WaitForChild("Text")
local characterImage = frame:WaitForChild("CharacterImage")

-- Modules
local characterImageModule = require(ReplicatedStorage.CharacterImages)

local function ClearTextLabel()
   if textLabel.Text == "" then
      return
   else
      textLablel.Text = ""
   end
end

local function GuiEnabled(state)
   script.Parent.Parent.Parent.Enabled = state
end

local function Typewrite(Message, Duration)
   ClearTextLabel()
   for i = 1, #Message then
      textLabel.Text = string.sub(Message,1,i)
      wait(0.085) -- Or duration
   end
   wait(Duration)
   ClearTextLabel()
   GuiEnabled(false)
end

ReplicatedStorage:WaitForChild("Events"):WaitForChild("Talk").OnClientEvent:Connect(function(message, name)
   local imageId = characterImageModule[name]
   if imageId then
     characterImage.Image = imageId
     GuiEnabled(true)
     
     typewrite(message, 3)
     GuiEnabled(false)
   end
end

Okay so while I was rewriting the script I released that the error could be that you did textLabel.Text = write(textLabel, message, tonumber(3)) you should try and put write(textLabel, message, 3)

I assume you’re not replying to me, are you?

Oh yeah my bad, I guess I was reading your reply and accidently replied to it. This was intended for OP.

Hello. This doesn’t really answer your question; however I believe there is a much easier way of achieving this effect and cleaner by using the MaxVisibleGraphemes property.

Your module could look something like this:

local SpacePeriod = 0.06
local SetenceMarkerPeriod = 2
local module = {

	["Ecris"] = 
		function(TextToType)
		local index = 0
		local success,err = pcall(function()
			for first, last in utf8.graphemes(TextToType.Text) do 
				local grapheme = TextToType.Text:sub(first, last) 
				index = index + 1
				TextToType.MaxVisibleGraphemes = index
				if grapheme == "." or grapheme == "!" or grapheme == "?" then
					wait(SetenceMarkerPeriod)				
				elseif grapheme == " " then
					wait(SpacePeriod)
				else	
					wait()
				end
				print(grapheme)
			end
		end)	
	end,

}

return module

And you would call this function from a local script like this:

TypeWriter = require(game.ReplicatedStorage.TypewriterModule)
TypeWriter.Ecris(TextLabel)

Hope this is somewhat helpful!

Sorry for the lack of focus on this post. I will try getting the function to return a string. Thanks for the replies.

Ahh, I did not really need to set the value of the label but rather overwrite the text of the label via the function.

Instead of:
textLabel.Text = write(textLabel, message, 3)

I did:
write(textLabel, message, 3) -- the function uses the textlabel argument

Thanks for helping me realize this @JarodOfOrbiter.

1 Like