Problems With Making a Script Work in a ModuleScript

Script is for an NPC talking system.
My script works fine, until I put it in a ModuleScript. Why is this?
Error: Players.RipPBB_TUD.PlayerGui.NPCTalk.TalkModule:26: attempt to index nil with ‘Character’ - Client - TalkModule:26
The error occurs every time I click on the NPC.

Am I doing something wrong with my ModuleScript? I don’t understand why it can’t find the character.

Code with ModuleScript
Module
local talk = {}

talk.talkfunction = function()
	script.Parent.Enabled = false
	local textlabel1 = script.Parent:WaitForChild("Main"):WaitForChild("Text1")
	local textlabel2 = script.Parent:WaitForChild("Main"):WaitForChild("Text2")
	--local lines = require:(game.StarterPlayer.StarterCharacterScripts:WaitForChild(Lines)
	local npcs = game.Workspace.NPCs
	local Dummy = npcs.Dummy
	local page = 1
	local proceed = script.Parent:WaitForChild("Main"):WaitForChild("Proceed")
	local pagedone = false
	local lastpage = false



	local function typewrite(object,text)
		for i = 1,#text,1 do
			object.Text = string.sub(text,1,i)
			game:GetService("RunService").Stepped:Wait(0.001)
		end
	end

	local function dummyText(player)
		if script.Parent.Enabled == false then
			local char = player.Character
			local hum = char.Humanoid
			hum.JumpPower = 0
			hum.WalkSpeed = 0
			hum.AutoRotate = false
			page = 1
			pagedone = false
			lastpage = false
			script.Parent.Enabled = true
			typewrite(textlabel1,"This is a long long long long long line of random text.")
			typewrite(textlabel2,"This is a test for the 2nd line test test test.")
			pagedone = true
			proceed.MouseButton1Click:Connect(function()
				if lastpage and pagedone then
					script.Parent.Enabled = false
					textlabel1.Text = ""
					textlabel2.Text = ""
					hum.JumpPower = 50
					hum.WalkSpeed = 16
					hum.AutoRotate = true
				elseif page ==1 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					page = 2
					pagedone = false
					lastpage = false
					typewrite(textlabel1, "This is the 1st line of the 2nd page!")
					typewrite(textlabel2, "This is the 2nd line of the 2nd page!")
					pagedone = true
					pagedone = true
				elseif page == 2 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					pagedone = false
					page = 3

					typewrite(textlabel1, "This is the 1st line of the 3rd page!")
					typewrite(textlabel2, "This is the 2nd line of the 3rd page!")
					pagedone = true
					page = 1
					lastpage = true
				end
			end)
		else

		end

	end
dummyText()

	


end

return talk
LocalScript
local talkmodule = require(script.Parent.TalkModule)
local npcs = game.Workspace.NPCs
local Dummy = npcs.Dummy
Dummy.Click.ClickDetector.MouseClick:Connect(talkmodule.talkfunction)
Code without ModuleScript
script.Parent.Enabled = false
local textlabel1 = script.Parent:WaitForChild("Main"):WaitForChild("Text1")
local textlabel2 = script.Parent:WaitForChild("Main"):WaitForChild("Text2")
--local lines = require:(game.StarterPlayer.StarterCharacterScripts:WaitForChild(Lines)
local npcs = game.Workspace.NPCs
local Dummy = npcs.Dummy
local page = 1
local proceed = script.Parent:WaitForChild("Main"):WaitForChild("Proceed")
local pagedone = false
local lastpage = false



local function typewrite(object,text)
	for i = 1,#text,1 do
		object.Text = string.sub(text,1,i)
		game:GetService("RunService").Stepped:Wait(0.001)
	end
end

local function dummyText(player)
	if script.Parent.Enabled == false then
		local char = player.Character
		local hum = char.Humanoid
		hum.JumpPower = 0
		hum.WalkSpeed = 0
		hum.AutoRotate = false
		page = 1
		pagedone = false
		lastpage = false
		script.Parent.Enabled = true
		typewrite(textlabel1,"This is a long long long long long line of random text.")
		typewrite(textlabel2,"This is a test for the 2nd line test test test.")
		pagedone = true
		proceed.MouseButton1Click:Connect(function()
			if lastpage and pagedone then
				script.Parent.Enabled = false
				textlabel1.Text = ""
				textlabel2.Text = ""
				hum.JumpPower = 50
				hum.WalkSpeed = 16
				hum.AutoRotate = true
			elseif page ==1 and pagedone then
				textlabel1.Text = ""
				textlabel2.Text = ""
				page = 2
				pagedone = false
				lastpage = false
				typewrite(textlabel1, "This is the 1st line of the 2nd page!")
				typewrite(textlabel2, "This is the 2nd line of the 2nd page!")
				pagedone = true
				pagedone = true
			elseif page == 2 and pagedone then
				textlabel1.Text = ""
				textlabel2.Text = ""
				pagedone = false
				page = 3

				typewrite(textlabel1, "This is the 1st line of the 3rd page!")
				typewrite(textlabel2, "This is the 2nd line of the 3rd page!")
				pagedone = true
				page = 1
				lastpage = true
			end
		end)
	else
		
	end
	
end


Dummy.Click.ClickDetector.MouseClick:Connect(dummyText)

I’m not sure why I’m getting an error, could someone help me? Thank you!

The error has been fixed, but now a couple lines of the script aren’t working.

Link to post:

1 Like

when you originally call dummyText you aren’t passing the player value through. The modulescript reads it as nil, you cant get nothing’s character

I pointed it out here

local talk = {}

talk.talkfunction = function(player) --Pass the player into the modulescript
	script.Parent.Enabled = false
	local textlabel1 = script.Parent:WaitForChild("Main"):WaitForChild("Text1")
	local textlabel2 = script.Parent:WaitForChild("Main"):WaitForChild("Text2")
	--local lines = require:(game.StarterPlayer.StarterCharacterScripts:WaitForChild(Lines)
	local npcs = game.Workspace.NPCs
	local Dummy = npcs.Dummy
	local page = 1
	local proceed = script.Parent:WaitForChild("Main"):WaitForChild("Proceed")
	local pagedone = false
	local lastpage = false



	local function typewrite(object,text)
		for i = 1,#text,1 do
			object.Text = string.sub(text,1,i)
			game:GetService("RunService").Stepped:Wait(0.001)
		end
	end

	local function dummyText(player)
		if script.Parent.Enabled == false then
			local char = player.Character
			local hum = char.Humanoid
			hum.JumpPower = 0
			hum.WalkSpeed = 0
			hum.AutoRotate = false
			page = 1
			pagedone = false
			lastpage = false
			script.Parent.Enabled = true
			typewrite(textlabel1,"This is a long long long long long line of random text.")
			typewrite(textlabel2,"This is a test for the 2nd line test test test.")
			pagedone = true
			proceed.MouseButton1Click:Connect(function()
				if lastpage and pagedone then
					script.Parent.Enabled = false
					textlabel1.Text = ""
					textlabel2.Text = ""
					hum.JumpPower = 50
					hum.WalkSpeed = 16
					hum.AutoRotate = true
				elseif page ==1 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					page = 2
					pagedone = false
					lastpage = false
					typewrite(textlabel1, "This is the 1st line of the 2nd page!")
					typewrite(textlabel2, "This is the 2nd line of the 2nd page!")
					pagedone = true
					pagedone = true
				elseif page == 2 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					pagedone = false
					page = 3

					typewrite(textlabel1, "This is the 1st line of the 3rd page!")
					typewrite(textlabel2, "This is the 2nd line of the 3rd page!")
					pagedone = true
					page = 1
					lastpage = true
				end
			end)
		else

		end

	end
dummyText(player) --Then pass it into the function

	


end

return talk
1 Like

How would I pass it through then? I don’t really get it.

When this line fires

dummyText()

it doesnt pass anything into the function

local function dummyText(player)

when you put something in parentheses then it will listen for what gets passed through and set that as a variable, if you don’t pass anything through, then it will be nil.

This page on the Roblox Developer Hub should provide more details

1 Like

I don’t really know what to put for my case though, could you help me out?

I tried this, but it doesn’t work:
dummyText(player)

Sure, Here would be your localscript

local talkmodule = require(script.Parent.TalkModule)
local npcs = game.Workspace.NPCs
local Dummy = npcs.Dummy
local Player = game.Players.LocalPlayer
Dummy.Click.ClickDetector.MouseClick:Connect(function()
	talkmodule.talkfunction(Player) --Passing Player into the modulescript
end)

and the modulescript

local talk = {}

talk.talkfunction = function(Player) --Need to set player as a parameter
	script.Parent.Enabled = false
	local textlabel1 = script.Parent:WaitForChild("Main"):WaitForChild("Text1")
	local textlabel2 = script.Parent:WaitForChild("Main"):WaitForChild("Text2")
	--local lines = require:(game.StarterPlayer.StarterCharacterScripts:WaitForChild(Lines)
	local npcs = game.Workspace.NPCs
	local Dummy = npcs.Dummy
	local page = 1
	local proceed = script.Parent:WaitForChild("Main"):WaitForChild("Proceed")
	local pagedone = false
	local lastpage = false



	local function typewrite(object,text)
		for i = 1,#text,1 do
			object.Text = string.sub(text,1,i)
			game:GetService("RunService").Stepped:Wait(0.001)
		end
	end

	local function dummyText(player)
		if script.Parent.Enabled == false then
			local char = player.Character
			local hum = char.Humanoid
			hum.JumpPower = 0
			hum.WalkSpeed = 0
			hum.AutoRotate = false
			page = 1
			pagedone = false
			lastpage = false
			script.Parent.Enabled = true
			typewrite(textlabel1,"This is a long long long long long line of random text.")
			typewrite(textlabel2,"This is a test for the 2nd line test test test.")
			pagedone = true
			proceed.MouseButton1Click:Connect(function()
				if lastpage and pagedone then
					script.Parent.Enabled = false
					textlabel1.Text = ""
					textlabel2.Text = ""
					hum.JumpPower = 50
					hum.WalkSpeed = 16
					hum.AutoRotate = true
				elseif page ==1 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					page = 2
					pagedone = false
					lastpage = false
					typewrite(textlabel1, "This is the 1st line of the 2nd page!")
					typewrite(textlabel2, "This is the 2nd line of the 2nd page!")
					pagedone = true
					pagedone = true
				elseif page == 2 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					pagedone = false
					page = 3

					typewrite(textlabel1, "This is the 1st line of the 3rd page!")
					typewrite(textlabel2, "This is the 2nd line of the 3rd page!")
					pagedone = true
					page = 1
					lastpage = true
				end
			end)
		else

		end

	end
dummyText(Player) --Now when you fire dummyText pass Player through as a parameter

	


end

return talk
1 Like

It still gives me the same error.
Module:

local talk = {}

talk.talkfunction = function()
	script.Parent.Enabled = false
	local textlabel1 = script.Parent:WaitForChild("Main"):WaitForChild("Text1")
	local textlabel2 = script.Parent:WaitForChild("Main"):WaitForChild("Text2")
	--local lines = require:(game.StarterPlayer.StarterCharacterScripts:WaitForChild(Lines)
	local npcs = game.Workspace.NPCs
	local Dummy = npcs.Dummy
	local page = 1
	local proceed = script.Parent:WaitForChild("Main"):WaitForChild("Proceed")
	local pagedone = false
	local lastpage = false



	local function typewrite(object,text)
		for i = 1,#text,1 do
			object.Text = string.sub(text,1,i)
			game:GetService("RunService").Stepped:Wait(0.001)
		end
	end

	local function dummyText(player)
		if script.Parent.Enabled == false then
			local char = player.Character
			local hum = char.Humanoid
			hum.JumpPower = 0
			hum.WalkSpeed = 0
			hum.AutoRotate = false
			page = 1
			pagedone = false
			lastpage = false
			script.Parent.Enabled = true
			typewrite(textlabel1,"This is a long long long long long line of random text.")
			typewrite(textlabel2,"This is a test for the 2nd line test test test.")
			pagedone = true
			proceed.MouseButton1Click:Connect(function()
				if lastpage and pagedone then
					script.Parent.Enabled = false
					textlabel1.Text = ""
					textlabel2.Text = ""
					hum.JumpPower = 50
					hum.WalkSpeed = 16
					hum.AutoRotate = true
				elseif page ==1 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					page = 2
					pagedone = false
					lastpage = false
					typewrite(textlabel1, "This is the 1st line of the 2nd page!")
					typewrite(textlabel2, "This is the 2nd line of the 2nd page!")
					pagedone = true
					pagedone = true
				elseif page == 2 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					pagedone = false
					page = 3

					typewrite(textlabel1, "This is the 1st line of the 3rd page!")
					typewrite(textlabel2, "This is the 2nd line of the 3rd page!")
					pagedone = true
					page = 1
					lastpage = true
				end
			end)
		else

		end

	end
dummyText(player)

	


end

return talk

Script:

local talkmodule = require(script.Parent.TalkModule)
local npcs = game.Workspace.NPCs
local Dummy = npcs.Dummy
Dummy.Click.ClickDetector.MouseClick:Connect(function()
	talkmodule.talkfunction(player)
end)

in the function parentheses type player
talk.talkfunction = function(Player)
and in the localscript set a variable for player

local talkmodule = require(script.Parent.TalkModule)
local npcs = game.Workspace.NPCs
local Dummy = npcs.Dummy
local player = game.Players.LocalPlayer
Dummy.Click.ClickDetector.MouseClick:Connect(function()
	talkmodule.talkfunction(player)
end)
1 Like

Still doesn’t work, here is the code:

local talk = {}

talk.talkfunction = function(player)
	script.Parent.Enabled = false
	local textlabel1 = script.Parent:WaitForChild("Main"):WaitForChild("Text1")
	local textlabel2 = script.Parent:WaitForChild("Main"):WaitForChild("Text2")
	--local lines = require:(game.StarterPlayer.StarterCharacterScripts:WaitForChild(Lines)
	local npcs = game.Workspace.NPCs
	local Dummy = npcs.Dummy
	local page = 1
	local proceed = script.Parent:WaitForChild("Main"):WaitForChild("Proceed")
	local pagedone = false
	local lastpage = false



	local function typewrite(object,text)
		for i = 1,#text,1 do
			object.Text = string.sub(text,1,i)
			game:GetService("RunService").Stepped:Wait(0.001)
		end
	end

	local function dummyText(player)
		if script.Parent.Enabled == false then
			local char = player.Character
			local hum = char.Humanoid
			hum.JumpPower = 0
			hum.WalkSpeed = 0
			hum.AutoRotate = false
			page = 1
			pagedone = false
			lastpage = false
			script.Parent.Enabled = true
			typewrite(textlabel1,"This is a long long long long long line of random text.")
			typewrite(textlabel2,"This is a test for the 2nd line test test test.")
			pagedone = true
			proceed.MouseButton1Click:Connect(function()
				if lastpage and pagedone then
					script.Parent.Enabled = false
					textlabel1.Text = ""
					textlabel2.Text = ""
					hum.JumpPower = 50
					hum.WalkSpeed = 16
					hum.AutoRotate = true
				elseif page ==1 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					page = 2
					pagedone = false
					lastpage = false
					typewrite(textlabel1, "This is the 1st line of the 2nd page!")
					typewrite(textlabel2, "This is the 2nd line of the 2nd page!")
					pagedone = true
					pagedone = true
				elseif page == 2 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					pagedone = false
					page = 3

					typewrite(textlabel1, "This is the 1st line of the 3rd page!")
					typewrite(textlabel2, "This is the 2nd line of the 3rd page!")
					pagedone = true
					page = 1
					lastpage = true
				end
			end)
		else

		end

	end
dummyText(player)

	


end

return talk
local talkmodule = require(script.Parent.TalkModule)
local npcs = game.Workspace.NPCs
local Dummy = npcs.Dummy
Dummy.Click.ClickDetector.MouseClick:Connect(function()
	talkmodule.talkfunction(player) 
end)

Try setting a variable for player in your localscript

local talkmodule = require(script.Parent.TalkModule)
local npcs = game.Workspace.NPCs
local Dummy = npcs.Dummy
local player = game.Players.LocalPlayer
Dummy.Click.ClickDetector.MouseClick:Connect(function()
	talkmodule.talkfunction(player) 
end)
1 Like

My NPC talking works, but it’s buggy.
The code is supposed to let the player only click on the NPC and run the text animation once, but it doesn’t work, do you know why? It also doesn’t run this code:

Thanks! (the code does normally work)

Is the parent for the modulescript the same as the one for the localscript? If not you could also pass the parent through as a different parameter

Modulescript

local talk = {}

talk.talkfunction = function(player, ScriptParent)
	ScriptParent.Enabled = false
	local textlabel1 = script.Parent:WaitForChild("Main"):WaitForChild("Text1")
	local textlabel2 = script.Parent:WaitForChild("Main"):WaitForChild("Text2")
	--local lines = require:(game.StarterPlayer.StarterCharacterScripts:WaitForChild(Lines)
	local npcs = game.Workspace.NPCs
	local Dummy = npcs.Dummy
	local page = 1
	local proceed = script.Parent:WaitForChild("Main"):WaitForChild("Proceed")
	local pagedone = false
	local lastpage = false



	local function typewrite(object,text)
		for i = 1,#text,1 do
			object.Text = string.sub(text,1,i)
			game:GetService("RunService").Stepped:Wait(0.001)
		end
	end

	local function dummyText(player)
		if script.Parent.Enabled == false then
			local char = player.Character
			local hum = char.Humanoid
			hum.JumpPower = 0
			hum.WalkSpeed = 0
			hum.AutoRotate = false
			page = 1
			pagedone = false
			lastpage = false
			script.Parent.Enabled = true
			typewrite(textlabel1,"This is a long long long long long line of random text.")
			typewrite(textlabel2,"This is a test for the 2nd line test test test.")
			pagedone = true
			proceed.MouseButton1Click:Connect(function()
				if lastpage and pagedone then
					script.Parent.Enabled = false
					textlabel1.Text = ""
					textlabel2.Text = ""
					hum.JumpPower = 50
					hum.WalkSpeed = 16
					hum.AutoRotate = true
				elseif page ==1 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					page = 2
					pagedone = false
					lastpage = false
					typewrite(textlabel1, "This is the 1st line of the 2nd page!")
					typewrite(textlabel2, "This is the 2nd line of the 2nd page!")
					pagedone = true
					pagedone = true
				elseif page == 2 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					pagedone = false
					page = 3

					typewrite(textlabel1, "This is the 1st line of the 3rd page!")
					typewrite(textlabel2, "This is the 2nd line of the 3rd page!")
					pagedone = true
					page = 1
					lastpage = true
				end
			end)
		else

		end

	end
dummyText(player)

	


end

return talk

and the localscript like

local talkmodule = require(script.Parent.TalkModule)
local npcs = game.Workspace.NPCs
local Dummy = npcs.Dummy
local player = game.Players.LocalPlayer
Dummy.Click.ClickDetector.MouseClick:Connect(function()
	talkmodule.talkfunction(player, script.Parent) 
end)

If the localscript is inside something and the modulescript inside something else like replicatedstorage then the modulescript’s parent would be replicatedstorage

It’s in the same thing.
image

I tried basically what you did but it worked for me :confused:

Try putting a print at the beginning of the modulescript like

talk.talkfunction = function(player)
	script.Parent.Enabled = false
	print(script.Parent.Enabled)

also try putting it after the module function because in your localscript it would run as soon as the localscript ran, not in the click event
like this

local talk = {}
script.Parent.Enabled = false
1 Like

Thanks! Everything works, except for my part of the code I made so that the text animations don’t run multiple times.

Like in there.
When I click multiple times, it still runs multiple times, instead of one.

Could you help me out on that? The script is supposed to work, I don’t get why it isn’t.

You aren’t using your debounce right. Its just checks is a value is false then does it, you need to set it to true during the loop

local debounce = false
local function Thing()
   if debounce == false then
      denounce = true
      --stuff
      --stuff
      --more stuff
      denounce = false
   end
end
1 Like

Hmm… it doesn’t seem to be running anymore.

Added a warn to make sure it doesn’t run.

Code:
local talk = {}
script.Parent.Enabled = false
talk.talkfunction = function(player)
	local dbounce = false
	script.Parent.Enabled = false
--	print(script.Parent.Enabled)
	local textlabel1 = script.Parent:WaitForChild("Main"):WaitForChild("Text1")
	local textlabel2 = script.Parent:WaitForChild("Main"):WaitForChild("Text2")
	--local lines = require:(game.StarterPlayer.StarterCharacterScripts:WaitForChild(Lines)
	local npcs = game.Workspace.NPCs
	local Dummy = npcs.Dummy
	local page = 1
	local proceed = script.Parent:WaitForChild("Main"):WaitForChild("Proceed")
	local pagedone = false
	local lastpage = false



	local function typewrite(object,text)
		for i = 1,#text,1 do
			object.Text = string.sub(text,1,i)
			game:GetService("RunService").Stepped:Wait(0.001)
		end
	end

	local function dummyText(player)
		
		if script.Parent.Enabled and dbounce == false then
			dbounce = true
			warn("Began")
			local char = player.Character
			local hum = char.Humanoid
			hum.JumpPower = 0
			hum.WalkSpeed = 0
			hum.AutoRotate = false
			page = 1
			pagedone = false
			lastpage = false
			script.Parent.Enabled = true
			typewrite(textlabel1,"This is a long long long long long line of random text.")
			typewrite(textlabel2,"This is a test for the 2nd line test test test.")
			pagedone = true
			proceed.MouseButton1Click:Connect(function()
				if lastpage and pagedone then
					script.Parent.Enabled = false
					textlabel1.Text = ""
					textlabel2.Text = ""
					hum.JumpPower = 50
					hum.WalkSpeed = 16
					hum.AutoRotate = true
					dbounce = false
				elseif page ==1 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					page = 2
					pagedone = false
					lastpage = false
					typewrite(textlabel1, "This is the 1st line of the 2nd page!")
					typewrite(textlabel2, "This is the 2nd line of the 2nd page!")
					pagedone = true
					pagedone = true
				elseif page == 2 and pagedone then
					textlabel1.Text = ""
					textlabel2.Text = ""
					pagedone = false
					page = 3

					typewrite(textlabel1, "This is the 1st line of the 3rd page!")
					typewrite(textlabel2, "This is the 2nd line of the 3rd page!")
					pagedone = true
					page = 1
					lastpage = true
				end
			end)
		else

		end

	end
dummyText(player)

	


end

return talk

LocalScript:

local talkmodule = require(script.Parent.TalkModule)

local npcs = game.Workspace.NPCs

local Dummy = npcs.Dummy

local player = game.Players.LocalPlayer

Dummy.Click.ClickDetector.MouseClick:Connect(function()

talkmodule.talkfunction(player)
end)

Do you know why? It’s probably a simple mistake in my code, but I can’t find it.

Thanks!

@Wizard101fire90

Without the debounce, it seems to be completely ignoring the

if script.Parent.Enabled == false then

And just runs the things inside it.
When I use the debounce,

if script.Parent.Enabled and dbounce == false then

It just doesn’t run at all.

My code’s in my other post up above.

What I see is that the debounce is being set to false inside the main function of the ModuleScript. If that happens then the debounce will not do anything.

When the localscript runs the function it immediately sets the debounce back to false.

I would suggest putting the debounce in the localscript instead of the modulescript

1 Like