Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Trying to call the same function in one module script?

Asked by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years ago

After finishing a basic setup for an NPC system, I've stumbled upon a obstacle trying to figure out how to call the same function that is in a Module Script from one local script. Basically the local script calls for the module script function system.newNPC to Clone the NPC model in Replicated Storage, place it in Workspace where it belongs, and have it be able to interact with the player.

Everything works but my problem is having the local script create more NPCs by calling that function in the module script. I read on wiki that calling a function in a Module Script more than once would return the same value. What is the work around for this in my case?

I didn't include everything in the Module Script for the sake of length but if I need to include it feel free to ask.

Module Script:

--located in game.ReplicatedStorage.Modules.NPCHandler
local system = {}
function system.newNPC(npcType, spawnLoc, msg, cameraAngle)

end
return system

Local Script:

--located in game.StarterGui
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local npcHandler = game.ReplicatedStorage.Modules.NPCHandler

--local worldNPCSpawns = game.Workspace:WaitForChild("NPCFolder"):WaitForChild("worldSpawns")

local npcSpawner = require(npcHandler).newNPC("npc", game.Workspace.npcSpawn,
    {   "Hello there",
        "This is a test message",
        "You are here to talk to me?"
    },
     game.Workspace.npcCamera);

--Removes NPCs if player dies.
character.Humanoid.Died:connect(function()
    npcSpawner()
end)

After that I edited the above local script, and what I wanted to do was something like this:

--located in game.StarterGui
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local npcHandler = game.ReplicatedStorage.Modules.NPCHandler

--local worldNPCSpawns = game.Workspace:WaitForChild("NPCFolder"):WaitForChild("worldSpawns")

local npcRobert = require(npcHandler).newNPC("npc", game.Workspace.npcRobertSpawn,
    {   "Hello there",
        "This is a test message",
        "Is there something you need?"
    },
     game.Workspace.npcRobertCamera);

local npcDave = require(npcHandler).newNPC("npc", game.Workspace.npcDaveSpawn,
    {   "Hello I'm Dave",
        "This is a test message",
        "How are you doing."
    },
     game.Workspace.npcDaveCamera);

 --In my module script there is something that should disconnect the player from anything connecting to my module script. This next code recalls the functions above to disconnect if the player dies.
character.Humanoid.Died:connect(function()
    npcRobert()
    npcDave()
end)

What I get from this above script is that the NPCs spawn where they're supposed to but for reason they only one NPC can interact with the player.

more: If there are any things I should look into for this that would be helpful but if you can explain to me how to go about this then that would be amazing. Thanks.

Answer this question