Hi! I’m having a small problem with this NPCs.
I want it to sit and then jump, however it gets stuck with the sit animation while it is floating, and it is not even moving for some reason.
Code:
-- Chooses Chair --
local ChosenChair
if script.Parent.Parent.Name == "Workspace" then
ChosenChair = workspace.Chairs:GetChildren()[math.random(1, #workspace.Chairs:GetChildren())]
end
local Seat = ChosenChair:WaitForChild("Seat")
script.Parent.Seat.Value = Seat
-- Path to Chair --
local Torso = script.Parent:WaitForChild("Torso")
local Human = script.Parent:WaitForChild("Humanoid")
local LookingToSit = true
local LaunchEvent = true
function wait(TimeToWait)
if TimeToWait ~= nil then
local TotalTime = 0
TotalTime = TotalTime + game:GetService("RunService").Heartbeat:wait()
while TotalTime < TimeToWait do
TotalTime = TotalTime + game:GetService("RunService").Heartbeat:wait()
end
else
game:GetService("RunService").Heartbeat:wait()
end
end
function CalculateDistance()
local Distance = (Torso.Position - Seat.Position).Magnitude
if Distance < 5 then
print("Sitting.")
Seat:Sit(Human)
LookingToSit = false
script.Parent.needToFixJump.Value = false
wait()
if LaunchEvent == true then
wait(math.random(10,15))
print("Launched!")
script.Parent.Status.Value = "Home"
LaunchEvent = false
wait()
script.Disabled = true
end
else
LookingToSit = true
script.Parent.needToFixJump.Value = true
end
end
function Move()
local PathArgs = {
["AgentHeight"] = 5;
["AgentRadius"] = 2;
["AgentCanJump"] = true;
}
local Path = game:GetService("PathfindingService"):CreatePath()
Path:ComputeAsync(Torso.Position, Seat.Position)
local Waypoints = Path:GetWaypoints()
if Path.Status == Enum.PathStatus.Success then
for _, Waypoint in pairs(Waypoints) do
if Waypoint.Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid.Jump = true
end
Human:MoveTo(Waypoint.Position)
Human.MoveToFinished:Wait()
end
else
warn("Path unsuccessful, destroying path.")
end
end
while true do
wait()
CalculateDistance()
if LookingToSit == true then
Move()
else
break
end
end