Hi, I've been at this for hours. I teach kids game development, and they've recently been asking to do a Roblox mod. I don't know how to do this, but I figured I can make a lesson plan over the next few months so we can do it next year. I'm trying to get an NPC to move, but I'm having trouble.
Here is my code currently:
function Enemy:OnCreate()
while (true) do wait(2) local target = self:GetNearestPlayer() if (target) then local torso = target:GetTorso() self.object.NPC.Humanoid:MoveTo(torso.Position, torso) end end
end
I am using CodeKingdoms because it is easier for my students to understand. If someone could help me out that would be awesome. Thanks!
Hello, austeefrostee!
If you want the NPC to teleport somewhere, then you're doing almost correctly... Just a little change needed
function Enemy:OnCreate() while (true) do wait(2) local target = self:GetNearestPlayer() if (target) then local torso = target:GetTorso() self.object.NPC:MoveTo(torso.Position) -- ":MoveTo" is not a Function of the Humanoid, but of the Model end end end
But, if you want the NPC to WALK to the player, then you should do this:
function Enemy:OnCreate() while (true) do wait(2) local target = self:GetNearestPlayer() if (target) then local torso = target:GetTorso() self.object.NPC.Humanoid:WalkToPoint(torso.Position) -- ":WalkToPoint" makes the humanoid walk to a position(in this case, player's torso) end end end
Useful links:
https://developer.roblox.com/api-reference/property/Humanoid/WalkToPoint
https://developer.roblox.com/api-reference/property/Humanoid/WalkToPart
https://developer.roblox.com/api-reference/function/Model/MoveTo
PS: I'm using normal roblox codes(for roblox studio, game creation), I don't know if this will work for a mod