Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my textlabel typewrite script give a error?

local textlabel = script.Parent.TextHere
local name = script.Parent.Name

wait(1)

local function typewrite(object,text)
	for i = 1,#text,1 do
		object.Text = string.sub(text,1,i)
		wait(0.025)
	end
end

typewrite(textlabel,"HEY! This is private property! Don't make-")
wait(2)
textlabel.TextXAlignment = 2
textlabel.TextYAlignment = 1
typewrite(textlabel,"Wait...")
wait(2)
typewrite(textlabel,"You- You're HIM!")
wait(2)
textlabel.TextXAlignment = 0
textlabel.TextYAlignment = 0
typewrite(textlabel,"I'm sorry, that wasn't very nice of me. I'm sure you're a amazing human being!")
wait(3)
textlabel.TextXAlignment = 2
textlabel.TextYAlignment = 1
typewrite(textlabel,"M-My name is Xanathaniel.")
typewrite(name,"Xanathaniel")

This is the Dialog script, the possible problem is about line 28, I'll paste the error here too.

Players.Mercyvr.PlayerGui.ConversationGui.LocalScript:8: attempt to index string with 'Text' - Client - LocalScript:8

It's either this Error, or a error about line 28.

1 answer

✓ Accepted answer

On line 2 you are defining Name as a string, name of the parent, then on line 28 you call the function typewrite, however that function expects TextLabel or TextButton as first parameter because it then changes it's .Text property. You are however, inputting string so it indexes string and then errors because it's not valid member of string.

If you want to change text of script.Parent then change second line to

local name = script.Parent

This should refer to the parent of the script.


If however name variable should refer to TextLabel that is inside of script.Parent, you would need to use FindFirstChild or better WaitForChild which both will ensure that you are searching for a child

local textlabel = script.Parent:WaitForChild("TextHere")
local name = script.Parent:WaitForChild("Name")

[...]

FindFirstChild looks for children and returns nil if it's not found while WaitForChild will yield until a child with given name is found. You should always use WaitForChild for LocalScripts inside of StarterGui as they are often not yet replicated when script runs.

This discussion is preserved as it was. New answers, comments, and votes are closed.