My code is not running

Hello, my code won’t run I’m trying to make an NPC follow a “path” you create by clicking.
Game file can be provided but I’d prefer not sharing it if possible.

Create Paths(Local Script):

local point1pos = game:GetService("ReplicatedStorage").Point1Position.Value

local pointnum = game:GetService("ReplicatedStorage").PointNumber.Value
local point = game:GetService("ReplicatedStorage").Point

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local mouse = player:GetMouse()
local mousepos = mouse.Hit.Position

mouse.Button1Down:Connect(function()
	point1pos = mousepos
	mousepos = mouse.Hit.Position
	local part = point:Clone()
	part.Parent = workspace.Folder
	part.Position = mousepos
	part.Name = ("Point"..pointnum)
	game:GetService("ReplicatedStorage").AddNumber:FireServer(player, mousepos)
end)

Spawn NPC(Local Script):

local point1pos = game:GetService("ReplicatedStorage").Point1Position.Value

local button = script.Parent

button.Activated:Connect(function()
	local NPC = game:GetService("ReplicatedStorage").Bob:Clone()
	NPC.Parent = workspace
	wait(.5)
	NPC.Script.Enabled = true
end)

Server Events(Script):

local repstorage = game:GetService("ReplicatedStorage")

local point1pos = repstorage.Point1Position or repstorage:WaitForChild("Point1Position")

local pointnum = repstorage.PointNumber or repstorage:WaitForChild("PointNumber")
local point = repstorage.Point or repstorage:WaitForChild("Point")

local number = repstorage.PointNumber or repstorage:WaitForChild("PointNumber")

local add = repstorage.AddNumber or repstorage:WaitForChild("AddNumber")
local reset = repstorage.ResetNumber or repstorage:WaitForChild("ResetNumber")
add.OnServerEvent:Connect(function()
	number.Value += 1
end)
reset.OnServerEvent:Connect(function()
	number.Value = 1
end)

local create = repstorage.CreatePart or repstorage:WaitForChild("CreatePart")
create.OnServerEvent:Connect(function(player, mousepos)
	wait(.1)
	point1pos = mousepos
	local part = point:Clone()
	part.Parent = workspace.Folder
	part.Position = mousepos
	part.Name = ("Point"..pointnum.Value)
end)

NPC Pathfind(Script):

local ps = game:GetService("PathfindingService")
local humanoid = script.Parent.Parent.Humanoid

wait(1)
wait(.5)
for i, v in pairs(workspace.Folder:GetChildren()) do
	print("w")
	humanoid:MoveTo(v.CFrame)
	wait(1)
end


Bob is the NPC. Somebody please help.

Can’t say this is the only problem but when you do FireServer you don’t send the player value it gets added automatically so just do:

game:GetService("ReplicatedStorage").AddNumber:FireServer(mousepos)

Seems like you might really want CreatePart here instead as you wouldn’t need mousepos to just increment the number. If that is the case you don’t need to create the part in the local script as it will get created by your server script.

The issue is with Bob because he needs to be able to pathfind but if all the paths are in the player scope but not the server scope, which he operates in, then he can’t see the paths right? Would it maybe work if the pathfind script was a local script?

Correct. If Bob is in the server then just allow the server to create the parts you don’t need to create them locally. Your Spawn NPC says it is a local script though which is fine if you want only the local player to see Bob and in that case you only need to create the points locally and no need to use createPart server event.

I tried switching Bob to the server with another remote event my bad if that’s bad, and now he appears but still doesn’t run. I tried using test to see the server POV and the server cannot see the paths.

Is this how your create paths local script looks now?

Also in your NPC Pathfind script instead of print("w") try printing something a bit more useful like print(v.Name, v.Position).

If you don’t call CreatePart to create the parts on the server, Bob has nothing to follow.

I had forgotten to remove the localscript part creation. I have removed it now, but now neither the player nor the server can see the paths. Also< I changed the print and it still had no output. I also do think I have CreatePart on the server as shown here:

local repstorage = game:GetService("ReplicatedStorage")

local point1pos = repstorage.Point1Position or repstorage:WaitForChild("Point1Position")

local pointnum = repstorage.PointNumber or repstorage:WaitForChild("PointNumber")
local point = repstorage.Point or repstorage:WaitForChild("Point")

local number = repstorage.PointNumber or repstorage:WaitForChild("PointNumber")

local add = repstorage.AddNumber or repstorage:WaitForChild("AddNumber")
local reset = repstorage.ResetNumber or repstorage:WaitForChild("ResetNumber")
add.OnServerEvent:Connect(function()
	number.Value += 1
end)
reset.OnServerEvent:Connect(function()
	number.Value = 1
end)

local start = repstorage.CreateBob or repstorage:WaitForChild("CreateBob")
local create = repstorage.CreatePart or repstorage:WaitForChild("CreatePart")
create.OnServerEvent:Connect(function(player, mousepos)
	wait(.1)
	point1pos = mousepos
	local part = point:Clone()
	part.Parent = workspace.Folder
	part.Position = mousepos
	part.Name = ("Point"..pointnum.Value)
end)
start.OnServerEvent:Connect(function()
	local NPC = game:GetService("ReplicatedStorage").Bob:Clone()
	NPC.Parent = workspace
	wait(.5)
	NPC.Script.Enabled = true
	NPC:MoveTo(workspace.Folder:WaitForChild("Point1"))
end)

That is the script in ServerScriptStorage.

I understand the code to generate the part is on the server, but the reason I asked if you code is the way I posted is because if you don’t fire the event anywhere it doesn’t get called and therefore doesn’t create the parts. Did you update your create paths script with the call to CreatePart?

Okay now the parts are showing up but Bob still isn’t moving(and no ouput either). I edited a few other things too, and now I have this error saying “Unable to cast CoordinateFrame to Vector3” server script 8 which is bobs script.

Change this:

humanoid:MoveTo(v.CFrame)

to this:

humanoid:MoveTo(v.Position)

MoveTo takes a Vector3 not a CFrame

Thanks for helping me out! -------

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.