I am making a game where I need zombies. So I decided I would start on the zombie. I created the model and currently scripting functionality for it. So I am stuck on the part where I need to make the whole entire zombie move to my location. I got that working, but not how I wanted. The problem is only the HumanoidRootPart is following me instead of the whole zombie model. So is there any reason why this is happening? Do I need to set a PrimaryPart to my zombie model in order for this to work? If I do need to set a primary part what part should I set it to and what in my code do I need to change?
ZombieScript
local self = {}
local maxDistance = 25
local function FindClosestPlayer(zombie)
local players = game:GetService("Players"):GetPlayers()
if #players ~= 0 then
for _, player in ipairs(players) do
if player.Character then
local distance = (zombie.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).magnitude
return player
end
end
end
end
local function AttachAiToZombie(zombie)
spawn(function()
while wait() do
local player = FindClosestPlayer(zombie)
local zombieHumanoid = zombie:FindFirstChild("Humanoid")
if zombieHumanoid and player then
zombieHumanoid:MoveTo(player.Character.HumanoidRootPart.Position)
end
wait(0.1)
end
end)
end
function self:CreateZombie(position)
local zombie = game.ReplicatedStorage.Assets.ZombiePrefab:Clone()
zombie.Parent = workspace.Map.Enemies
zombie:SetPrimaryPartCFrame(CFrame.new(position))
AttachAiToZombie(zombie)
end
return self
Testing script
local zombieScript = require(game.ServerScriptService.Modules.ZombieScript)
zombieScript:CreateZombie(Vector3.new(0, 10, 0))
EDIT: I found the answer to my question. I had to set the PrimaryPart to my torso then use the SetPrimaryPartCFrame function.
