Accessing values from Table is Nil

I am attempting to put all of the children of a folder into a table, with each child containing a String value that will serve as the text for dialogue when that part is touched. It will take the first part in the table and then delete it after it has been used, and so on.

As I have never used tables much, I’m confused as to what is wrong here, but it says that the value in the table I’m trying to access is nil.
This script is a local script inside of a text label in a screenGUI in startGUI.


Can’t find what’s wrong.

1 Like

I’m pretty certain this is happening because the game hasn’t loaded the folder and it’s children in completely when you’re looping through, so maybe try adding this to the top of the script:

repeat task.wait(1) until game.Loaded

I would also like to mention that you should change local text = part.Value to local text = part.Value.Value since you’re referencing the StringValue instance and not the StringValue’s actual value. You should change the name of “Value” to “TextValue” and changing the code to part.TextValue.Value, just to be more organized and make the code less confusing visually.

3 Likes

This helped fix that main issue, but I now realize that something with my algorithm is probably off, as the first part I touched played text and then deleted, but the 2nd part + all further parts did not display text on touch.

(This is the current version of the script and folder)
My intended algorithm is to remove the first value in the table and the new first is what displays text incase I scripted it in a way that is hard to tell what I intended.

It says StringValue for the 2nd part is nil.

That’s because you only ever reference the 1st index in the table, that being the first part in the folder. Heres a script that does what you ask. Replace part.Touched function with the code below and put all of the code inside of your “touched” function into the code below; right under text = v:FindFirstChildWhichIsA("StringValue").Value

for Index,Part in pairs(info) do
	Part.Touched:Connect(function()
		local text = Part:FindFirstChildWhichIsA("StringValue").Value
		
	end)
end

I would also like to mention that using that repeat until is unnecessary as you can just use a Tween; which basically does exactly what you’re trying to achieve with the repeat until.
Heres code of the tween if you would like to do that instead. Place this at the top of the script:

local TweenService = game:GetService("TweenService")
local TweenOptions = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0) -- you can change any of these options to your liking

Replace the repeat until with this:

local TweenTextTransparency = TweenService:Create(script.Parent,TweenOptions,{TextTransparency = 1})
TweenTextTransparency:Play()

TweenTextTransparency.Completed:Connect(function() -- detects when the tween ends
    db = false
end)

Final script with the tween:

repeat task.wait(1) until game.Loaded
local typeSound = game.SoundService.letterType
local mainFolder = game.Workspace.textTouchParts
local db = false

local TweenService = game:GetService("TweenService")
local TweenOptions = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)

local info = {}
for i,v in pairs(mainFolder:GetChildren()) do
	table.insert(info,v)
end

for Index,Part in pairs(info) do
	Part.Touched:Connect(function()
		local text = Part:FindFirstChildWhichIsA("StringValue").Value
		if db == false then
			db = true
			for i = 1, #text do
				script.Parent.Text = string.sub(text,1,i)
				typeSound:Play()
				task.wait(0.075)
			end
			task.wait(2)
			local TweenTextTransparency = TweenService:Create(script.Parent,TweenOptions,{TextTransparency = 1})
			TweenTextTransparency:Play()
			TweenTextTransparency.Completed:Connect(function()
				db = false
			end)
		end
        table.remove(info,Index)
		Part:Destroy()
	end)
end
1 Like

This makes more sense. It works, thank you!

1 Like

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