Hey there y’all!
I’m trying to make a NPC character to go to the your destinations also known as the paths. I watched @.GnomeCode’s video to make it called “AI Character Pathfinding”
The scripted in the first part of the video was working very normally, the character started to walk to the paths but while changing the script from the beginning to the end of the tutorial, the AI script started to stop working.
I tried to see If I didn’t see some errors or forgot to add some more details but none of them made the script work again. His NPC worked very well but mine don’t. Here’s the scripts from the video to the end:
The beginning script: (Also, the NPC is called Johnny)
local johnny = script.Parent
local humanoid = johnny.Humanoid
while true do
humanoid:MoveTo(workspace.destination.Position)
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(workspace.destination2.Position)
humanoid.MoveToFinished:Wait()
end
And here’s the final changes of the AI script:
local humanoid = johnny.Humanoid
local PathfindingService = game:GetService("PathfindingService")
local function getPath(destination)
local pathParams = {
["AgentHeight"] = 7,
["AgentRadius"] = 5.2
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(johnny.HumanoidRootPart.Position, destination.Position)
return path
end
local function walkTo(destination)
local path = getPath(destination)
for index, waypoint in pairs(path:GetWaypoints)) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
local function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum].Position)
end
while wait(0.5) do
patrol()
end
Video with the first script:
robloxapp-20210401-0124151.wmv
And the video with the final changes:
robloxapp-20210401-0126277.wmv
(If it’s lagging, bugging or having bad quality in the video, it’s because of the Roblox Recorder that somehow looks like that I have a Nokia phone)
And I don’t know how If I need to make the first script and add some details like “while wait (0.5)…” to make the NPC work or need to delete stuffs that can make errors. If I need to change, I really don’t know what I need since I don’t understand script codes and your specific languages to program stuffs but if y’all can help me out with this, I would appreciate that very much!
Thank you all!
- Juan