Dialogue Class erroring nil on self.NPC for some reason

I made a dialogue class (WIP) and I tried testing it but I came with an issue I normally never get it.

I am guessing this is a really small error that is not very noticeable to me

Dialogue:39: attempt to index nil with 'Name'

Class Module:


function Dialogue.new(NPC: Instance, DialogueText: string, QuestOn: boolean, QuestName)
	local self = setmetatable({},Dialogue)
	
	self.NPC = NPC
	self.Text = DialogueText
	self.QuestOn = QuestOn
	self.QuestName = QuestName
	
	print(self.NPC) -- it works here but it doesnt work on dialogue:createui
	return self
end

function Dialogue:CreateUI() -- screengui is default disabled 
	local ScreenGUI = Instance.new("ScreenGui")
	ScreenGUI.Name = "Dialogue "..self.NPC.Name -- the issue is right here
	
	local MainFrame = Instance.new("Frame")
	MainFrame.AnchorPoint = Vector2.new(0.5,0.5)
	MainFrame.Position = UDim2.new(0.5,0,0.5,0)
	MainFrame.Size = UDim2.new(1,0,1,0)
	MainFrame.BackgroundColor3 = Color3.new(0,0,0)
	MainFrame.BackgroundTransparency = 0.2
	
	local NameFrame = Instance.new("Frame")
	NameFrame.AnchorPoint = Vector2.new(0.,0.5)
	NameFrame.Position = UDim2.new(0.241,0,0.619,0)
	NameFrame.Size = UDim2.new(0.195,0,0.07,0)
	
	local NameText = Instance.new("TextLabel")
	NameText.AnchorPoint = Vector2.new(0.5,0.5)
	NameText.Position = UDim2.new(0.5,0,0.5,0)
	NameText.Size = UDim2.new(1,0,1,0)
	NameText.Text = self.NPC.Name
	
	local TextFrame = Instance.new("Frame")
	TextFrame.AnchorPoint = Vector2.new(0.,0.5)
	TextFrame.Position = UDim2.new(0.536,0,0.744,0)
	TextFrame.Size = UDim2.new(0.72,0,0.229,0)
	
	local Yes = Instance.new("Frame")
	Yes.AnchorPoint = Vector2.new(0.,0.5)
	Yes.Position = UDim2.new(0.353,0,0.915,0)
	Yes.Size = UDim2.new(0.354,0,0.07,0)
	
	local YesText = Instance.new("TextLabel")
	YesText.AnchorPoint = Vector2.new(0.5,0.5)
	YesText.Position = UDim2.new(0.5,0,0.5,0)
	YesText.Size = UDim2.new(1,0,1,0)
	YesText.TextColor3 = Color3.fromRGB(112, 255, 111)
	YesText.Font = "Source Sans Pro" or Enum.Font.SourceSans 
	YesText.TextScaled = true
	YesText.Text = "YES"
	
	local No = Instance.new("Frame")
	No.AnchorPoint = Vector2.new(0.,0.5)
	No.Position = UDim2.new(0.719,0,0.915,0)
	No.Size = UDim2.new(0.354,0,0.07,0)
	
	local NoText = Instance.new("TextLabel")
	NoText.AnchorPoint = Vector2.new(0.5,0.5)
	NoText.Position = UDim2.new(0.5,0,0.5,0)
	NoText.Size = UDim2.new(1,0,1,0)
	NoText.TextColor3 = Color3.fromRGB(250, 67, 46)
	NoText.Font = "Source Sans Pro" or Enum.Font.SourceSans 
	NoText.TextScaled = true
	YesText.Text = "NO"
	
	local Ok = Instance.new("Frame")
	Ok.AnchorPoint = Vector2.new(0.,0.5)
	Ok.Position = UDim2.new(0.536,0,0.915,0)
	Ok.Size = UDim2.new(0.72,0,0.07,0)
	
	local OkText = Instance.new("TextLabel")
	OkText.AnchorPoint = Vector2.new(0.5,0.5)
	OkText.Position = UDim2.new(0.5,0,0.5,0)
	OkText.Size = UDim2.new(1,0,1,0)
	OkText.TextColor3 = Color3.fromRGB(112, 255, 111)
	OkText.Font = "Source Sans Pro" or Enum.Font.SourceSans 
	OkText.TextScaled = true
	OkText.Text = "OK"
	
	-- DEFAULT BOOLEANS (DONT CHANGE)
	ScreenGUI.Enabled = false 
	if not self.QuestOn then
		Yes.Visible = false -- yes for quest
		No.Visible = false -- no for quest
	end
	
	MainFrame.Parent = ScreenGUI
	
	NameFrame.Parent = MainFrame
	NameText.Parent = MainFrame
	
	TextFrame.Parent = MainFrame
	
	Yes.Parent = MainFrame
	YesText.Parent = Yes
	
	No.Parent = MainFrame
	NoText.Parent = No
	
	Ok.Parent = MainFrame
	OkText.Parent = Ok
	
	for i,v in pairs(MainFrame:GetChildren()) do
		if v:IsA("Frame") then
			local UICorner = Instance.new("UICorner")
			local UIStroke = Instance.new("UIStroke")
			UICorner.Parent = v
			UIStroke.Parent = v
			
			v.BackgroundColor3 = Color3.new(0,0,0)
			v.BackgroundTransparency = 0.2
		end
	end

	return ScreenGUI
end

How I started the dialogue (Client)

v.ClickDetector.MouseClick:Connect(function()
			local NewDialogue = Dialogue.new(v,v.Dialogue.Text,false,nil)
			
			NewDialogue:Start()
		end)

also createui is ran somewhere, i just didnt show the full class

2 Likes

maybe because this is local to that function not the full script

either that or maybe not being sent over by client correctly

2 Likes

isn’t it already being returned?

I removed the local and it still errored. Here’s how it’s being sent over:

local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
local Dialogue = require(script.Parent.Parent.Modules.Dialogue)

local Tag = "NPC" -- don't change it if you already tagged ur npcs...

local DialogueController = Knit.CreateController{
	Name = "DialogueController"
}

function DialogueController:KnitInit()
	print(script.Name.." Initilized.")
end

function DialogueController:KnitStart()
	for i,v in pairs(game:GetService("CollectionService"):GetTagged("NPC")) do
		--if not v:FindFirstChild("") then
		--	warn("BodyColors not found inside of:",v.Name.." | Are you sure", v.Name.." is an NPC?")
		--	return 
		--end
		
		if not v.ClickDetector then
			warn("ClickDetector not found inside of:",v.Name.." | Add a clickdetector inside of", v.Name)
			return 
		end
		
		if not v.Dialogue then
			warn("Dialogue Folder not found inside of:",v.Name.." | Check the instructions inside of the dialogue module")
			return 
		end
		
		v.ClickDetector.MouseClick:Connect(function()
			local NewDialogue = Dialogue.new(v,v.Dialogue.Text,false,"h")
			
			NewDialogue:Start()
		end)
		
	end
end

return DialogueController
1 Like

nevermind see where you state it prints

2 Likes

how are you declaring the top of the module?

1 Like

top of the module is this:

local Dialogue = {}
Dialogue.__index = Dialogue

local RichText = require(script.RichText)

function Dialogue.new(NPC: Instance, DialogueText: string, QuestOn: boolean, QuestName)
	local self = setmetatable({},Dialogue)
	
	self.NPC = NPC
	self.Text = DialogueText
	self.QuestOn = QuestOn
	self.QuestName = QuestName
	
-- i removed the print
	return self
end
1 Like

may try it like this

local DataTable = {
	NPC = NPC,
	Text = DialogueText,
	QuestOn = QuestOn,
	QuestName = QuestName,
}
local self = setmetatable(DataTable, Dialogue)
2 Likes

Didn’t work… I am looking as well I’m so confused

function Dialogue.new(NPC: Instance, DialogueText: string, QuestOn: boolean, QuestName)
	local DataTable = {
		NPC = NPC,
		Text = DialogueText,
		QuestOn = QuestOn,
		QuestName = QuestName,
	}

	local self = setmetatable(DataTable, Dialogue)
	
	return self
end

same error btw

1 Like

where are you calling the createui from?

from starterplayerscripts, why?

I moved everything from starterplayerscripts to replicatedservice and it still did the same error

Still no solutions :slight_smile: pls help!!!

Are you sure you tagged the right part? Make sure the part is tagged with NPC and not a model it’s contained in. I suggest changing the parameter NPC:Instance to NPC:BasePart if that’s what you’re expecting.

I found the solution

The problem was I accidentally put createui function as : instead of normal. it ignores self when you do that for some reason


function CreateUI(NPC, QuestOn) -- screengui is default disabled 
	local ScreenGUI = Instance.new("ScreenGui")
	ScreenGUI.Name = "Dialogue "..NPC.Name

	local MainFrame = Instance.new("Frame")
	MainFrame.AnchorPoint = Vector2.new(0.5,0.5)
	MainFrame.Position = UDim2.new(0.5,0,0.5,0)
	MainFrame.Size = UDim2.new(1,0,1,0)
	MainFrame.BackgroundColor3 = Color3.new(0,0,0)
	MainFrame.BackgroundTransparency = 0.2

	local NameFrame = Instance.new("Frame")
	NameFrame.AnchorPoint = Vector2.new(0.,0.5)
	NameFrame.Position = UDim2.new(0.241,0,0.619,0)
	NameFrame.Size = UDim2.new(0.195,0,0.07,0)

	local NameText = Instance.new("TextLabel")
	NameText.AnchorPoint = Vector2.new(0.5,0.5)
	NameText.Position = UDim2.new(0.5,0,0.5,0)
	NameText.Size = UDim2.new(1,0,1,0)
	NameText.Text = NPC.Name

	local TextFrame = Instance.new("Frame")
	TextFrame.AnchorPoint = Vector2.new(0.,0.5)
	TextFrame.Position = UDim2.new(0.536,0,0.744,0)
	TextFrame.Size = UDim2.new(0.72,0,0.229,0)

	local Yes = Instance.new("Frame")
	Yes.AnchorPoint = Vector2.new(0.,0.5)
	Yes.Position = UDim2.new(0.353,0,0.915,0)
	Yes.Size = UDim2.new(0.354,0,0.07,0)

	local YesText = Instance.new("TextLabel")
	YesText.AnchorPoint = Vector2.new(0.5,0.5)
	YesText.Position = UDim2.new(0.5,0,0.5,0)
	YesText.Size = UDim2.new(1,0,1,0)
	YesText.TextColor3 = Color3.fromRGB(112, 255, 111)
	YesText.Font = Enum.Font.SourceSans 
	YesText.TextScaled = true
	YesText.Text = "YES"

	local No = Instance.new("Frame")
	No.AnchorPoint = Vector2.new(0.,0.5)
	No.Position = UDim2.new(0.719,0,0.915,0)
	No.Size = UDim2.new(0.354,0,0.07,0)

	local NoText = Instance.new("TextLabel")
	NoText.AnchorPoint = Vector2.new(0.5,0.5)
	NoText.Position = UDim2.new(0.5,0,0.5,0)
	NoText.Size = UDim2.new(1,0,1,0)
	NoText.TextColor3 = Color3.fromRGB(250, 67, 46)
	NoText.Font = Enum.Font.SourceSans 
	NoText.TextScaled = true
	YesText.Text = "NO"

	local Ok = Instance.new("Frame")
	Ok.AnchorPoint = Vector2.new(0.,0.5)
	Ok.Position = UDim2.new(0.536,0,0.915,0)
	Ok.Size = UDim2.new(0.72,0,0.07,0)

	local OkText = Instance.new("TextLabel")
	OkText.AnchorPoint = Vector2.new(0.5,0.5)
	OkText.Position = UDim2.new(0.5,0,0.5,0)
	OkText.Size = UDim2.new(1,0,1,0)
	OkText.TextColor3 = Color3.fromRGB(112, 255, 111)
	OkText.Font = Enum.Font.SourceSans 
	OkText.TextScaled = true
	OkText.Text = "OK"

	-- DEFAULT BOOLEANS (DONT CHANGE)
	ScreenGUI.Enabled = false 

	if not QuestOn then
		Yes.Visible = false -- yes for quest
		No.Visible = false -- no for quest
	end
	
	ScreenGUI.Parent = game.Players.LocalPlayer.PlayerGui
	MainFrame.Parent = ScreenGUI

	NameFrame.Parent = MainFrame
	NameText.Parent = MainFrame

	TextFrame.Parent = MainFrame

	Yes.Parent = MainFrame
	YesText.Parent = Yes

	No.Parent = MainFrame
	NoText.Parent = No

	Ok.Parent = MainFrame
	OkText.Parent = Ok

	for i,v in pairs(MainFrame:GetChildren()) do
		if v:IsA("Frame") then
			local UICorner = Instance.new("UICorner")
			local UIStroke = Instance.new("UIStroke")
			UICorner.Parent = v
			UIStroke.Parent = v

			v.BackgroundColor3 = Color3.new(0,0,0)
			v.BackgroundTransparency = 0.2
		end
	end

	return ScreenGUI
end

silly me

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