Attempt to index nil with number even though table exists

so i have a dialogue script but apparently when i try to provide diag[index] it says attempt to index nil with number???

idk whats going on heres the script and below script is error and printed table:

-- rescripted was here :)

-- // SERVICES
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- // VARIABLES
local mainFrame = script.Parent:WaitForChild("MainFrame")
local charImage = mainFrame:WaitForChild("Guy")
local charName	= mainFrame:WaitForChild("Yapper")
local dialouge	= mainFrame:WaitForChild("Yapping")
local options	= mainFrame:WaitForChild("Options")
local template	= script:WaitForChild("Template")

local typewriter = require(script:WaitForChild("TypewriterModule"))
local dialogueMessages = require(script:WaitForChild("DialogueMessages"))

-- // FUNCTIONS

local function Dialogue(name: string, text: string, imageId: number, optionTable: {any})
	charImage.Image = `rbxassetid://{imageId}`
	charName.Text = name
	
	for _, option in options:GetChildren() do
		if not option:IsA("GuiButton") then continue end
		option:Destroy() -- destroys every option before creating new options
	end
	
	typewriter.typewrite(dialouge, text)
	
	for _, option in optionTable do
		local newOption = template:Clone()
		newOption.Text = option[1]
		newOption.Parent = options
	end
	
	return optionTable
end

local function StartDialogue(npc): {any}
	mainFrame.Parent.Enabled = true
	npc:FindFirstChild("HumanoidRootPart"):FindFirstChildOfClass("ProximityPrompt").Enabled = false
	
	return Dialogue(
		npc.Name,
		dialogueMessages[npc.Name]["NPC"][1],
		dialogueMessages[npc.Name]["ImageId"],
		dialogueMessages[npc.Name]["Options"][1]
	)
end

local function NextLine(index, handler, npc): boolean
	if handler == "close" then
		mainFrame.Parent.Enabled = false
		npc:FindFirstChild("HumanoidRootPart"):FindFirstChildOfClass("ProximityPrompt").Enabled = true
		return true
	end
	print(dialogueMessages[npc.Name])
	Dialogue(
		npc.Name,
		dialogueMessages[npc.Name]["NPC"][index],
		dialogueMessages[npc.Name]["ImageId"],
		dialogueMessages[npc.Name]["Options"][index]
	)
	return false
end

local function addProximity(model: Model)
	assert(model:IsA("Model"), `invalid argument #1 to addProximity (Model expected, got {typeof(model)})`)
	print(model.Name)
	local prox = Instance.new("ProximityPrompt")
	prox.ActionText = "Talk"
	prox.KeyboardKeyCode = Enum.KeyCode.F
	prox.RequiresLineOfSight = false
	prox.MaxActivationDistance = 5
	prox.Parent = model:FindFirstChild("HumanoidRootPart")
	
	local debounce = false
	
	local index = 1
	local diag
	prox.Triggered:Connect(function()
		diag = StartDialogue(model)
	end)
	options.ChildAdded:Connect(function(option)
		if not option:IsA("GuiButton") then return end
		option.MouseButton1Click:Connect(function()
			if (charName.Text ~= model.Name) and debounce then return end
			debounce = true
			index += 1
			
			print(diag[index], diag, index) -- Line 91
			local reset = NextLine(index, diag[index][2], model)
			index = reset and 1

			task.wait(0.5)
			debounce = false
		end)
	end)
end

for _, npc in workspace:WaitForChild("Tidefall Bay"):WaitForChild("NPCS"):GetChildren() do
	if not npc:IsA("Model") then continue end
	addProximity(npc)
end

image

This means that the diag variable is nil, so trying to do diag[index] means you’re trying to do something like nil[1] which of course isn’t valid.

So then the first thing I’d look at is when we define diag, which is inside the prox.Triggered:Connect()... inline function. Is that running as expected? Is StartDialogue running and returning a table as expected?

but it printed so idk whats going on

yes, its running perfectly, and StartDialogue did return the table

Try printing these in this order (and on different lines):

print(diag)
print(index)
print(diag[index])

that might give you more info. I suspect it’ll still fail on that last line but let’s see what diag and index are coming as.

the first child prints diag as a table, but the second child prints diag as nil? how does it just turn to nil all of a sudden

EDIT: idk how and why but the MouseButton1Click signal fires 2 times when i connect it

Yeah if its inconsistent it sounds like something is happening twice. Weird the click is happening twice, maybe change to MouseButton1Down or MouseButton1Up instead? Sounds like that needs to be investigated a bit more.

still fires 2 times idk whats going on but its rly weird

EDIT: i tried printing the button name and it printed 4 times… *turns out it fires 2 times because theres 2 models so i might just check if the npc has a dialogue and that may work

yep, my mistake (another npc did not have a dialogue and it basically broke the script)

1 Like

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