I am a developer in a piggy fan game and another developer made this script. The script is working but the bot is walking into walls and I do not know why. It is a module script and doesn't require the module things. I can't check the height of the bot because my studio camera is fixed in this (if you do not know it means I cannot move the camera). Pretty much what it is meant to do is track the player and kill them once it reaches them (what npckill does) its annoying cause I can't get it to track the player and it just can't go around and it tries to go thru walls.

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local createPart = require(game:GetService("ServerScriptService").TempPart)

return function(rig)
	local hum = rig.Humanoid
	local root = rig.HumanoidRootPart
	
	for _, v in ipairs(rig:GetDescendants()) do
		if v:IsA("BasePart") and not v.Anchored then
			v:SetNetworkOwner(nil)
		end
	end
	
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = {rig}
	local function Raycast(origin, direction)
		local result = workspace:Raycast(origin, direction.Unit * 100, params)
		return result
	end
	
	local function GetGroundPosition(pos)
		local result = Raycast(pos, Vector3.FromNormalId(Enum.NormalId.Bottom))
		return result and result.Position or pos
	end
	
	require(script.Parent.NPCKill)(rig)
	local doorDebounce = true
	root.Touched:Connect(function(obj)
		if obj.Name == "DoorClick" and doorDebounce == true then
			doorDebounce = false
			local opened = obj.Parent:FindFirstChild("Opened")
			local hinge = obj.Parent:FindFirstChild("Hinge")
			local hingeOpened = obj.Parent:FindFirstChild("HingeOpened")
			local doorLock = obj:FindFirstChild("DoorLock")
			local doorOpen = obj:FindFirstChild("DoorOpen")
			if opened and hinge and hingeOpened and doorLock and doorOpen then
				if opened.Value == false then
					doorLock:Play()
					opened.Value = true

					hum.WalkSpeed = 0
					obj.CanCollide = false

					wait(.25)

					doorOpen:Play()
					TweenService:Create(hinge, TweenInfo.new(.35, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut),{CFrame = hingeOpened.CFrame}):Play()
					wait(.45)

					coroutine.resume(coroutine.create(function()
						wait(2)
						obj.CanCollide = true
					end))

					hum.WalkSpeed = 14
				end
			end
			doorDebounce = true
		end
	end)
	
	local target
	local targetPos
	local waypoints
	local currentWaypoint
	local t = 0
	local latestPathfind = tick()
	hum.MoveToFinished:Connect(function(reached)
		if not reached then
			hum.Jump = true
			hum:MoveTo(targetPos, target)
			print("Not reached")
		else
			if waypoints and currentWaypoint < #waypoints then
				currentWaypoint += 1
				if waypoints[currentWaypoint].Action == Enum.PathWaypointAction.Jump then
					hum.Jump = true
				end
				hum:MoveTo(waypoints[currentWaypoint].Position)
			else
				print("Reached")
			end
		end
	end)
	
	local function FindDoor(location)
		local parts = workspace:FindPartsInRegion3(Region3.new(location - Vector3.new(5, 2, 5), location + Vector3.new(5, 2, 5)))
		for _, v in ipairs(parts) do
			if v.Parent.Name == "Door" then
				return v.Parent.Frame.Part
			end
		end
	end
	
	local path = PathfindingService:CreatePath({AgentRadius = 3, AgentHeight = 7, AgentCanJump = true})
	RunService.Heartbeat:Connect(function(step)
		t += step
		if t > .5 then
			t -= .5
			-- get root parts
			local rootParts = {}
			for _, player in ipairs(Players:GetPlayers()) do
				local char = player.Character
				if workspace.PlayersAlive:FindFirstChild(player.Name) and char and char.Parent then
					local hum = char.Humanoid
					if hum:GetState() ~= Enum.HumanoidStateType.Dead then
						table.insert(rootParts, hum.RootPart)
					end
				end
			end
			-- find closest
			local MY_POSITION = root.Position
			local closest, distance = nil, 1000
			for _, rootPart in ipairs(rootParts) do
				local thisDistance = (rootPart.Position - MY_POSITION).magnitude
				if thisDistance < distance then
					closest, distance = rootPart, thisDistance
				end
			end
			if not closest then
				return
			end
			target = closest
			-- check if straight line, update filter
			local headPos = rig.Head.Position
			local targetHeadPos = target.Parent.Head.Position
			--[[
			createPart(groundPos)
			createPart(targetGroundPos)
			--]]
			
			--if not targetPos or (targetPos - targetHeadPos).magnitude > 3 or tick() - latestPathfind > 1 then
			if true then
				targetPos = targetHeadPos
				local canSee = Raycast(headPos, targetHeadPos - headPos)
				canSee = canSee and canSee.Instance
				path:ComputeAsync(root.Position, target.Position)
				if false then
					print(path.Status, canSee and canSee:GetFullName() or nil, headPos.Y, targetHeadPos.Y)
					--[[
					if canSee then
						local sel = canSee:FindFirstChild("SelectionBox")
						if not sel then
							sel = Instance.new("SelectionBox")
							sel.Adornee = canSee
							sel.SurfaceTransparency = .8
							sel.Parent = canSee
						end
					end
					--]]
				end
				local pos
				if canSee then
					local door = FindDoor(canSee.Position)
					if door then
						pos = door.Position
					elseif canSee:IsDescendantOf(target.Parent) or path.Status == Enum.PathStatus.NoPath then
						pos = targetHeadPos
					else
						print("No condition met")
					end
				end
				if pos then
					waypoints = nil
					-- :MoveTo
					hum:MoveTo(pos, target)
					print("MoveTo")
				else
					hum:MoveTo(targetHeadPos)
					-- Pathfind
					waypoints = path:GetWaypoints()
					if waypoints[1].Action == Enum.PathWaypointAction.Jump then
						hum.Jump = true
						currentWaypoint = 1
					else
						currentWaypoint = 2
					end
					if waypoints[currentWaypoint].Action == Enum.PathWaypointAction.Jump then
						hum.Jump = true
					end
					path.Blocked:Connect(function(waypointIndex)
						hum.Jump = true
						print("Blocked")
					end)
					hum:MoveTo(waypoints[currentWaypoint].Position)
				end
				latestPathfind = tick()
			end
		end
	end)
end

3 comments

0

you can make it jump by using a if statement: if waypoint.Action == Enum.PathWaypointAction.Jump then (YourNPCSHUMANOID):ChangeState(Enum.HumanoidStateType.Jumping then end

0

Closing this question because not enough detail etc.