MY NPCs weren't moving

Can anyone help me, please? I had struggled to figure out why the NPCs weren’t moving. I don’t know what to do.

Here’s code NPCBehavior:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AnimationFolder = ReplicatedStorage:WaitForChild("NPCAnimation")
local walkAnim = AnimationFolder:WaitForChild("WalkAnimation")

-- Bindable event for NPC behavior
local NPCBehaviorBindableEvent = 	ReplicatedStorage:WaitForChild("NPCRemote"):WaitForChild("NPCBehaviorEvent")

-- Function to make the NPC walk along its specific path
local function makeNPCWalk(npc, pathFolder)
	-- Ensure the NPC has a humanoid
	local humanoid = npc:FindFirstChildOfClass("Humanoid")
	if not humanoid then
		warn("NPC does not have a Humanoid! NPC Name: " .. npc.Name)
		return
	end
print("NPC has humanoid!")

-- Ensure the NPC has a PrimaryPart set (needed for MoveTo)
if not npc.PrimaryPart then
	warn("NPC does not have a PrimaryPart set! NPC Name: " .. npc.Name)
	return
end

-- Load and play the walk animation
local animTrack = humanoid:LoadAnimation(walkAnim)
animTrack:Play()
print("Walk animation started!")

-- Get all waypoints in the path folder
local waypoints = {}
for _, part in pairs(pathFolder:GetChildren()) do
	if part:IsA("Part") then
		table.insert(waypoints, part)
	end
end

if #waypoints == 0 then
	warn("No waypoints found for NPC: " .. npc.Name)
	return
end

print("Waypoints found: " .. #waypoints)

-- Sort waypoints by position (optional if you want a set order)
table.sort(waypoints, function(a, b)
	return a.Position.X < b.Position.X
end)

-- Make the NPC follow the waypoints
for _, waypoint in pairs(waypoints) do
	if humanoid and npc then
		print("Moving NPC to waypoint: " .. waypoint.Name)  -- Debugging print
		humanoid:MoveTo(waypoint.Position)
		-- Wait until the NPC reaches the waypoint
		humanoid.MoveToFinished:Wait()
		print("NPC reached waypoint: " .. waypoint.Name)  -- Debugging print
	end
end

-- Stop the walk animation when done
animTrack:Stop()
print("NPC finished walking.")  -- Debugging print
end

-- Listen for the BindableEvent to trigger NPC walking
NPCBehaviorBindableEvent.Event:Connect(function(npc, pathFolder)
	-- Ensure both npc and pathFolder are valid
	if npc and pathFolder then
		print("Triggering NPC walk: " .. npc.Name)  -- Debugging print
		makeNPCWalk(npc, pathFolder)
	else
		warn("NPC or PathFolder is nil!")
	end
end)

Tip for the future: instead of making 3 seperate topics and then deleting the 2 others, just edit your original post.

4 Likes

Is the NPCBehaviorBindableEvent being fired? can I see the script that fires it?

Sure thing,

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local NPCFolder = ReplicatedStorage:WaitForChild("NPCModels")
local ServerScriptService = game:GetService("ServerScriptService")
local NPCs_Local = game:GetService("Workspace"):WaitForChild("NPCs_Local")

-- Require the Clothings module
local Clothings = require(ServerScriptService:WaitForChild("NPCManager"):WaitForChild("Clothings"))

-- Bindable event for communicating between server-side scripts
local NPCBehaviorBindableEvent = Instance.new("BindableEvent")
NPCBehaviorBindableEvent.Name = "NPCBehaviorEvent"
NPCBehaviorBindableEvent.Parent = ReplicatedStorage:WaitForChild("NPCRemote")

-- Function to spawn an NPC at a specific position with a specified gender and NPC name
local function spawnNPC(spawnPosition, gender, npcName)
	local npcTemplate
	if gender == "Male" then
		npcTemplate = NPCFolder:FindFirstChild("NPCMaleModel")
	elseif gender == "Female" then
		npcTemplate = NPCFolder:FindFirstChild("NPCFemaleModel")
	end

	if not npcTemplate then
		warn("NPC model not found for gender: " .. gender)
		return
	end

	-- Clone the NPC and set the position
	local npcClone = npcTemplate:Clone()
	npcClone.Parent = NPCs_Local
	npcClone:SetPrimaryPartCFrame(CFrame.new(spawnPosition))

-- Rename the NPC based on the passed name
npcClone.Name = npcName

-- Call the Clothings module to customize the NPC's appearance
Clothings.applyClothing(npcClone, gender, npcName)

-- Get the waypoints folder for the NPC (e.g., "John_Waypoints")
local pathFolder = game.Workspace:WaitForChild("NPCWaypoints"):FindFirstChild(npcName .. "_Waypoints")
if not pathFolder then
 warn("No waypoints folder found for " .. npcName)
 return
end

-- Trigger the NPCBehaviorBindableEvent to make the NPC walk
print("Firing event to make NPC walk: " .. npcName)  -- Debugging print
NPCBehaviorBindableEvent:Fire(npcClone, pathFolder)  -- Passing the NPC and its path folder

-- Optionally, handle AI or other behaviors
print(npcName .. " has been spawned and is walking along the path.")
end

-- Auto-Spawn NPCs at Defined Locations
for _, spawnPoint in pairs(game.Workspace.NPCSpawns:GetChildren()) do
if spawnPoint:IsA("Part") then
	-- Check spawn points and create NPCs at specific locations
	if spawnPoint.Name == "Point1" then
		spawnNPC(spawnPoint.Position, "Male", "John")  -- Spawn a Male NPC named "John"
	elseif spawnPoint.Name == "Point2" then
		spawnNPC(spawnPoint.Position, "Female", "Lisa")  -- Spawn a Female NPC named "Lisa"
	end
  end
end
1 Like