Hello devs!
I’m trying to create a AI that turns on the light whenever the player turns off. But i’m having some problems on making it.
The npc returns normally to where he is but he comes back to the lightswitch after that.
Heres the code:
local npc = script.Parent
local goal = workspace.LightSwitch.WalkTo
local OldPos = Instance.new("Part")
OldPos.Position = script.Parent.HumanoidRootPart.Position
OldPos.Transparency = 1
OldPos.CanCollide = false
local pathfinding = game:GetService("PathfindingService")
function WalkToSwitch()
local path = pathfinding:CreatePath()
path:ComputeAsync(npc.HumanoidRootPart.Position, goal.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
npc.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
npc.Humanoid:MoveTo(waypoint.Position)
npc.Humanoid.MoveToFinished:Wait()
end
if goal.Parent.IsOn.Value == true then
print("Seriously?!?!?!")
wait(1)
elseif goal.Parent.IsOn.Value == false then
goal.Parent.IsOn.Value = true
wait(1)
end
local path2 = pathfinding:CreatePath()
path2:ComputeAsync(npc.HumanoidRootPart.Position, OldPos.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
npc.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
npc.Humanoid:MoveTo(waypoint.Position)
npc.Humanoid.MoveToFinished:Wait()
end
end
while wait(0.1) do
if goal.Parent.IsOn.Value == false then
print("Who turned the lights off!")
wait(1)
WalkToSwitch()
end
end