Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local PathfindingService = game:GetService("PathfindingService")
- local Waypoints = workspace.Waypoints:GetChildren()
- local AI = script.Parent
- local Humanoid = AI.Humanoid
- local HumanoidRootPart = AI.HumanoidRootPart
- local State = AI.Status
- local MovementMode = AI.MovementMode
- local currentlyMoving = false
- local breakCurrentWaypoint = false
- local movingFinished = script.MovingFinished
- local breakMoving = script.BreakMoving
- local raycastParams = RaycastParams.new()
- raycastParams.FilterType = Enum.RaycastFilterType.Exclude
- raycastParams.FilterDescendantsInstances = {AI}
- local lastwaypoint : BasePart = workspace.Baseplate
- function getRandomWaypoint() : BasePart & boolean
- local randomWaypoint : BasePart = Waypoints[math.random(1, #Waypoints)]
- if randomWaypoint.Name == lastwaypoint.Name then
- return getRandomWaypoint()
- end
- local inSight = waypointInSight(randomWaypoint)
- lastwaypoint = randomWaypoint
- return randomWaypoint, inSight
- end
- function waypointInSight(waypoint : BasePart)
- local direction = (waypoint.Position - HumanoidRootPart.Position).Unit * 1000
- local raycast = workspace:Raycast(HumanoidRootPart.Position, direction, raycastParams)
- if raycast then
- if string.find(raycast.Instance.Name, "Waypoint") then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- --[[
- function waypointIsBlocked(currentWaypoint: BasePart) : boolean
- local direction = (currentWaypoint.Position - HumanoidRootPart.Position).Unit * 1000
- local raycast = workspace:Raycast(HumanoidRootPart.Position, direction, raycastParams)
- if raycast then
- if raycast.Instance.Name == "WaypointPosition" then
- return false
- else
- return true
- end
- else
- return false
- end
- end]]
- function makePartOutOfWaypoint(waypointPosition: Vector3) : BasePart
- local part = Instance.new("Part", AI)
- part.Transparency = 1
- part.CanCollide = false
- part.Anchored = true
- part.Position = waypointPosition
- part.Name = "WaypointPosition"
- part.Size = Vector3.one
- return part
- end
- function changeStatsByState()
- if State.Value == "Walking" then
- Humanoid.WalkSpeed = 14
- elseif State.Value == "Running" then
- Humanoid.WalkSpeed = 20
- end
- end
- function chooseBehaviorAfterAction()
- local actions = {"BeIdle", "Continue"}
- local randomAction = actions[math.random(1, #actions)]
- return randomAction
- end
- function moveToWaypoint(waypoint: BasePart?, regeneratePath: boolean?)
- if not currentlyMoving then
- local waypoint, inSight = getRandomWaypoint()
- if waypoint then
- if inSight then
- currentlyMoving = true
- Humanoid:MoveTo(waypoint.Position, waypoint)
- Humanoid.MoveToFinished:Wait()
- currentlyMoving = false
- else
- local path = PathfindingService:CreatePath({
- AgentRadius = 2,
- AgentHeight = 5,
- AgentCanJump = false,
- AgentCanClimb = false
- })
- local success, errorMessage = pcall(function()
- path:ComputeAsync(HumanoidRootPart.Position, waypoint.Position)
- end)
- if success and path.Status == Enum.PathStatus.Success then
- currentlyMoving = true
- State.Value = "Walking"
- for _, pathWaypoint in ipairs(path:GetWaypoints()) do
- local pW_part = makePartOutOfWaypoint(pathWaypoint.Position)
- if breakCurrentWaypoint then
- break
- end
- if waypointInSight(waypoint) then
- MovementMode.Value = "Direct"
- Humanoid:MoveTo(pW_part.Position)
- Humanoid.MoveToFinished:Wait()
- break
- else
- if MovementMode.Value ~= "PathDependant" then
- MovementMode.Value = "PathDependant"
- end
- Humanoid:MoveTo(pW_part.Position)
- breakMoving.Event:Connect(function()
- breakCurrentWaypoint = true
- end)
- if breakCurrentWaypoint then
- break
- end
- Humanoid.MoveToFinished:Wait()
- end
- pW_part:Destroy()
- end
- movingFinished:Fire()
- currentlyMoving = false
- State.Value = "Idle"
- else
- State.Value = "Idle"
- end
- end
- end
- end
- end
- function update()
- moveToWaypoint()
- local action = chooseBehaviorAfterAction()
- if action then
- if action == "BeIdle" then
- State.Value = "Idle"
- task.wait(5)
- elseif action == "Continue" then
- State.Value = "Walking"
- end
- end
- update()
- end
- update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement