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.
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
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
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
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)
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)
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:
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
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
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.
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