I was creating a NPC just for practice and I was trying to make sure that if it needs to jump it will. But it is not working. I think it has to do something with the if statement. Here is the script:
local robloxiancat = script.Parent
local humanoid = robloxiancat:WaitForChild("Humanoid")
local rootPart = robloxiancat:WaitForChild("RootPart")
local Debris = game:GetService("Debris")
local PathFindingService = game:GetService("PathfindingService")
local pathArgs = {
["AgentHeight"] = 10,
["AgentRadius"] = math.ceil(1.5),
["AgentCanJump"] = true
}
local anim = robloxiancat:WaitForChild("Animation")
local animTrack = humanoid:LoadAnimation(anim)
animTrack.Looped = true
local scanRange = 50
local attackRange = 5
local function checkDistanceAB(a,b)
local magnitude = (a.Position - b.Position).Magnitude
return magnitude
end
local function attack(target)
local targetHumanoid = target.Parent:FindFirstChild("Humanoid")
local mag = checkDistanceAB(rootPart,target)
if mag < attackRange then
targetHumanoid.Health = targetHumanoid.Health - 1
wait(0.5)
print("In range")
if mag > attackRange then
print("Out of range")
return false
end
end
end
local function findTarget()
local target = nil
for index,value in ipairs(game.Workspace:GetChildren()) do
if value:IsA("Model") then
if value:FindFirstChild("HumanoidRootPart") and value:FindFirstChild("Humanoid") then
local targetRoot = value:FindFirstChild("HumanoidRootPart")
local targetHumanoid = value:FindFirstChild("Humanoid")
local magnitude = checkDistanceAB(rootPart,targetRoot)
if magnitude < scanRange and targetHumanoid.Health > 0 then
target = targetRoot
if target == rootPart then
print("Target is yourself, breaking")
break
else
print("Safe")
end
return target
end
end
end
end
end
local function MoveToTarget(target)
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath(pathArgs)
path:ComputeAsync(rootPart.Position,target.Position)
local waypoints = path:GetWaypoints()
print("Got points")
for _,waypoint in ipairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
print("Moving")
animTrack:Play()
if target.Parent:FindFirstChild("Humanoid").Health <= 0 or target.Parent.Parent ~= game.Workspace then
target = nil
print("Target died or left the game")
end
end
end
local function checkSight(target)
local RayOrigin = rootPart.Position
local magnitude = (RayOrigin - target.Position).Magnitude
local RayDirection = (target.Position - rootPart.Position).Unit * magnitude
local result = workspace:Raycast(RayOrigin,RayDirection)
if result then
local hit = result.Instance
if hit ~= target then
MoveToTarget(target)
else
print("Nothing is in NPC way")
MoveToTarget(target)
end
end
end
local function main()
local target = findTarget()
if target then
MoveToTarget(target)
checkSight(target)
if checkDistanceAB(rootPart,target) < attackRange then
attack(target)
end
end
end
while wait() do
if humanoid.Health > 0 then
main()
else
break
end
end