Hey there everyone!
I’ve been having a few issues with a script recently for an upcoming game I’m working on. I’ve been trying to get NPCs loaded locally into the game, so that the player can only see actions that they do with them. I’ve had no issue loading them in, and getting them found by the module script which is fired by a local script once the player joins the game.
However, I cannot seem to be able to animate the NPCs locally once the map is spawned and locally placed for the player.
There aren’t any errors coming up, and everytime I print something, it always prints. I’ve tried almost everything I could think of, from re-uploading the idle animation, to placing an animator inside of the humanoid.
Basically, all of the modules are located inside of ReplicatedStorage, including the MapChunks (So that the local scripts have access to them).
Then off in StarterGui, there is a single local script that starts the chain of scripts with this code:
Startup
local repstorage = game:GetService("ReplicatedStorage")
local _p = game.Players.LocalPlayer
repeat wait() until _p.Character -- wait until character loads in
local Chunkmodule = require(repstorage.Plugins:FindFirstChild("Chunk"))
Chunkmodule.OnLoadChunk("_chunktest")
Then, back into ReplicatedStorage where the “Chunk” module is located, where it loads in the chunk locally for the player. Along with this code:
ChunkModule
local chunkmodule = {}
local chunkfolder = game.ServerStorage:FindFirstChild("MapChunks")
local camera = game.Workspace.CurrentCamera
local npcmodule = require(script.Parent.NPC)
chunkmodule.OnLoadChunk = function(chunk)
local map = chunkfolder:FindFirstChild(chunk)
if not map then error(chunk.." not found") return end -- for debug
print('map found')
npcmodule.FindNPCs(map)
npcmodule.AnimateNPCs(map)
map:Clone().Parent = camera -- now load the map into the camera for local use
end
chunkmodule.UnLoadChunk = function(chunk)
local cam = game.Workspace.CurrentCamera
for i,v in pairs(cam:GetChildren()) do
if v.Name == chunk then
v:Destroy()
end
end
end
return chunkmodule
Once the chunk module finds the map, the NPCs are then found, then animated through the NPCModule:
FullNPCModule
local NPCModule = {}
function AddTag(NPC)
local tag = Instance.new("StringValue", NPC)
tag.Name = "#NPC"
end
local assetmodule = require(script.Parent.Parent.Data.Assets)
local vwalk, vpoint, vwave
NPCModule.AnimateNPCs = function(loadedchunk)
for i,v in pairs(loadedchunk:GetChildren()) do
if v:FindFirstChild("#NPC") and not v:FindFirstChild("NoAnimation") then
print(v.Name)
local vhum = v:FindFirstChild("Humanoid")
if not vhum then error(v.Name.." has no Humanoid") return end
local va = Instance.new("Animator", vhum)
vhum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
local vidlean = Instance.new("Animation", vhum)
vidlean.AnimationId = "rbxassetid://"..assetmodule.animationId.NPCIdle
local vwalkan = Instance.new("Animation", vhum)
vwalkan.AnimationId = "rbxassetid://"..assetmodule.animationId.NPCWalk
local vpointan = Instance.new("Animation")
vpointan.AnimationId = "rbxassetid://"..assetmodule.animationId.NPCPoint
local vwavean = Instance.new("Animation")
vwavean.AnimationId = "rbxassetid://"..assetmodule.animationId.NPCWave
local vidle = va:LoadAnimation(vidlean)
vwalk = vhum:LoadAnimation(vwalkan)
vpoint = vhum:LoadAnimation(vpointan)
vwave = vhum:LoadAnimation(vwavean)
vwalk:Play()
print('played')
end
end
end
NPCModule.FindNPCs = function(loadedchunk)
local map = loadedchunk
for i,v in pairs(map:GetChildren()) do
if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") then
AddTag(v)
elseif v:FindFirstChild("Humanoid") and not v:FindFirstChild("HumanoidRootPart") then -- these are just here to help with debugging
error(v.Name.." does not have a HumanoidRootPart")
elseif not v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") then
error(v.Name.." does not have a Humanoid")
end
end
end
return NPCModule
That’s how the NPC is loaded through these scripts so far. Like said in the replies, I haven’t made the NPCs go through Workspace themselves without going into the camera, and will change a few minor things as well.
If this adds anything to it, the “chunk” model location is ReplicatedStorage, and it is pulled from RS after the NPCs go through animating.
I’ve looked at countless DevForum posts about similar issues, but none of them have helped this issue I’ve been having.
If anybody has any suggestions/solutions, they would be much appreciated!