Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Erik's Companion Mode [Dec. 28 2023 Edition] --
- local SearchDistance = 2500
- function getHumanoid(model)
- for _, v in pairs(model:GetChildren()) do
- if v:IsA'Humanoid' then
- return v
- end
- end
- end
- local erikAI = script.Parent
- local human = getHumanoid(erikAI)
- local hroot = erikAI.HumanoidRootPart
- local zspeed = hroot.Velocity.magnitude
- local pfs = game:GetService("PathfindingService")
- function GetPlayerNames()
- local players = game:GetService('Players'):GetChildren()
- local name = nil
- for _, v in pairs(players) do
- if v:IsA'Player' then
- name = tostring(v.Name)
- end
- end
- return name
- end
- function GetPlayersBodyParts(t)
- local torso = t
- if torso then
- local figure = torso.Parent
- for _, v in pairs(figure:GetChildren()) do
- if v:IsA'Part' then
- return v.Name
- end
- end
- else
- return "HumanoidRootPart"
- end
- end
- function GetTorso(part)
- local chars = game.Workspace:GetChildren()
- local torso = nil
- for _, v in pairs(chars) do
- if v:IsA'Model' and v ~= erikAI and v.Name == GetPlayerNames() then
- local charRoot = v:FindFirstChild'HumanoidRootPart'
- if (charRoot.Position - part).magnitude < SearchDistance then
- torso = charRoot
- end
- end
- end
- return torso
- end
- for _, erikAIparts in pairs(erikAI:GetChildren()) do
- if erikAIparts:IsA'Part' then
- erikAIparts.Touched:Connect(function(p)
- if p.Parent.Name == GetPlayerNames() and p.Parent.Name ~= erikAI.Name then
- local target = p.Parent
- local targetHuman = getHumanoid(target)
- end
- end)
- end
- end
- local path
- local waypoint
- local oldpoints
- local isWandering = 0
- while wait() do
- local targetTorso = GetTorso(hroot.Position)
- if targetTorso ~= nil then
- isWandering = 1
- local function checkw(t)
- local ci = 3
- if ci > #t then
- ci = 3
- end
- if t[ci] == nil and ci < #t then
- repeat
- ci = ci + 1
- wait()
- until t[ci] ~= nil
- return Vector3.new(1, 0, 0) + t[ci]
- else
- ci = 3
- return t[ci]
- end
- end
- path = pfs:FindPathAsync(hroot.Position, targetTorso.Position)
- waypoint = path:GetWaypoints()
- oldpoints = waypoint
- local connection;
- local direct = Vector3.FromNormalId(Enum.NormalId.Front)
- local ncf = hroot.CFrame * CFrame.new(direct)
- direct = ncf.p.unit
- local rootr = Ray.new(hroot.Position, direct)
- local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot)
- if path and waypoint or checkw(waypoint) then
- if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
- human:MoveTo(checkw(waypoint).Position)
- human.Jump = false
- end
- if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
- human.Jump = true
- connection = human.Changed:Connect(function()
- human.Jump = true
- end)
- human:MoveTo(checkw(waypoint).Position)
- else
- human.Jump = false
- end
- hroot.Touched:Connect(function(p)
- local bodypartnames = GetPlayersBodyParts(targetTorso)
- if p:IsA'Part' and not p.Name == bodypartnames and phit and phit.Name ~= bodypartnames and phit:IsA'Part' and rootr:Distance(phit.Position) < 5 then
- connection = human.Changed:Connect(function()
- human.Jump = true
- end)
- else
- human.Jump = false
- end
- end)
- if connection then
- connection:Disconnect()
- end
- else
- for i = 3, #oldpoints do
- human:MoveTo(oldpoints[i].Position)
- end
- end
- elseif targetTorso == nil then
- path = nil
- waypoint = nil
- human.MoveToFinished:Wait()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement