i am making a horror game based on identity fraud, but when i near the npc…
IM JUST GONNA SHAKE SHAKE SHAKE SHAKE /mj
anyways, the problem is, the npc just… starts dancing around like crazy.
how can i fix this?
code:
local Character = script.Parent
local Humanoid = Character.Humanoid
local PathfindingService = game:GetService("PathfindingService")
Character.PrimaryPart:SetNetworkOwner(nil)
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 40
local nearestTarget
for i, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function GetPath(destination)
local path = PathfindingService:CreatePath()
path:ComputeAsync(Character.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 2 then
Humanoid:MoveTo(target.HumanoidRootPart.Position)
else
game.ReplicatedStorage.ChaseEnd:FireClient(game.Players:GetPlayerFromCharacter(target))
target.Humanoid.Health = 0
local newhumd = game.Players:GetHumanoidDescriptionFromUserId(game.Players:GetPlayerFromCharacter(target).UserId)
Humanoid:ApplyDescription(newhumd)
if Character.Head:FindFirstChild("face") then
Character.Head.face.Texture = "rbxassetid://" .. 16332986504
end
end
end
local function walkTo(destination)
local path = GetPath(destination)
if path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target then
Humanoid.WalkSpeed = 20
attack(target)
game.ReplicatedStorage.ChaseBegin:FireClient(game.Players:GetPlayerFromCharacter(target))
break
else
game.ReplicatedStorage.ChaseEnd:FireAllClients()
Humanoid.WalkSpeed = 16
Humanoid:MoveTo(waypoint.Position)
Humanoid.MoveToFinished:Wait()
end
end
else
Humanoid:MoveTo(destination.Position - (Character.HumanoidRootPart.CFrame.LookVector) * 10)
end
end
local function patrol()
local waypoints = workspace.Waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while wait() do
patrol()
end
--Humanoid:MoveTo(workspace.destination.Position)
--Humanoid.MoveToFinished:Wait()