I have no idea whats happening…
Sometimes, when my player gets caught by my monster ai, it glitches out the position. My script is very scuffed, so sorry if some things in it dont make sense.
local char = script.Parent
local humanoid = char.Humanoid
char.PrimaryPart:SetNetworkOwner(nil)
local pathFinder = game:GetService("PathfindingService")
local destinations = workspace.WorkerNightDestinations:GetChildren()
local always = true
local walkinganimation = script:WaitForChild("Walking")
local WalkAnim = humanoid:LoadAnimation(walkinganimation)
local chasingplayer = false
local TS = game:GetService("TweenService")
local RS = game:GetService("ReplicatedStorage")
local currenttarget = nil
--local RepS = game:GetService("ReplicatedStorage")
--local Players = game:GetService("Players")
--local LE = RepS:WaitForChild("LightEvent")
--local SLE = RepS:WaitForChild("StopLightEvent")
local clicking = char.HumanoidRootPart.Sound
local scream = char.HumanoidRootPart.Scream
clicking:Play()
local slow = false
WalkAnim:Play()
local fadeout = TS:Create(scream, TweenInfo.new(1.0, Enum.EasingStyle.Linear), {Volume = 0})
local function findNearestPlayer()
local nearestPlayer = nil
local shortestDistance = 200
for _, thing in pairs(workspace:GetChildren()) do
if thing:IsA("Model") and thing ~= char and thing.Name ~= "Rig" and thing:FindFirstChild("Humanoid") and thing.IsPatient.Value == true and thing.Humanoid.Health ~= 0 and thing.BreakingRule.Value == true then
thing.BeingChased.Value = true
currenttarget = thing
local hrp = thing.HumanoidRootPart
local distance = (char.HumanoidRootPart.Position - hrp.Position).Magnitude
if distance <= shortestDistance then
nearestPlayer = thing
shortestDistance = distance
end
end
end
return nearestPlayer
end
humanoid.Touched:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") and part.Parent.Name ~= "Worker" and part.Parent.Name ~= "InsaneDummy" and part.Parent.IsPatient.Value == true then
part.Parent.HumanoidRootPart.Position = game.Workspace:FindFirstChild(part.Parent.RoomOwned.Value).Position
part.Parent.BreakingRule.Value = false
part.Parent.Humanoid.WalkSpeed = 16
fadeout:Play()
RS.Jumpscare:FireClient(game:GetService("Players"):GetPlayerFromCharacter(part.Parent), "PatientCaught")
slow = false
wait(1)
part.Parent.Humanoid.WalkSpeed = 16
end
end)
local target = nil
local path = nil
local function SlowPlayer(target)
slow = true
RS.Jumpscare:FireClient(game:GetService("Players"):GetPlayerFromCharacter(target), "PatientDiscovered")
while target.Humanoid.WalkSpeed > 0 do
wait(0.1)
target.Humanoid.WalkSpeed -= 1
if slow == false then
target.Humanoid.WalkSpeed = 16
break
end
end
end
task.spawn(function()
while always do
--if chasingplayer == false then
-- WalkAnim:Stop()
--end
target = findNearestPlayer()
if not target then
target = destinations[math.random(1, #destinations)]
end
--if chasingplayer == false then
-- WalkAnim:Play()
--end
path = pathFinder:CreatePath()
if target:IsA("Model") and target.Humanoid.Health ~= 0 then
path:ComputeAsync(char.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
if chasingplayer == false then
scream.Volume = 7.797
scream:Play()
end
--local hitPlayer = Players:GetPlayerFromCharacter(target)
--LE:FireClient(hitPlayer)
chasingplayer = true
if slow ~= true then
SlowPlayer(target)
end
--RS.UnassignRoom:Fire(target.RoomOwned.Value, "X")
game.ServerStorage.Values.PlayerDied.Value = true
else
path:ComputeAsync(char.HumanoidRootPart.Position, target.Position)
if chasingplayer == true then
fadeout:Play()
chasingplayer = false
if currenttarget ~= nil then
currenttarget.BeingChased.Value = false
--local hitPlayer = Players:GetPlayerFromCharacter(currenttarget)
--SLE:FireClient(hitPlayer)
currenttarget = nil
end
end
WalkAnim:Stop()
wait(1)
WalkAnim:Play()
end
task.wait()
end
end)
game:GetService("RunService").Heartbeat:Connect(function(deltatime)
if char.Parent == game.Workspace then
for _, wayPoint in pairs(path:GetWaypoints()) do
char.Humanoid:MoveTo(wayPoint.Position)
--if wayPoint.Action == Enum.PathWaypointAction.Jump then
-- char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
--end
if target:IsA("Model") and target ~= char and target.Humanoid.Health ~= 0 and target.IsPatient.Value == true and (char.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude <= 2 then
print("Player found.")
chasingplayer = false
target.BeingChased.Value = false
char.Humanoid:MoveTo(target.HumanoidRootPart.Position)
break
end
end
end
end)