织梦CMS - 轻松建站从此开始!

欧博ABG官网-欧博官方网址-会员登入

欧博allbetNPC chase script

时间:2025-08-07 06:57来源: 作者:admin 点击: 9 次
Download file: NPC chase test.rbxl (34.7 KB) The code will loop through the players and check who is the closest distance away from the NPC. The NPC

SikeCSGO (kryddsalt) March 8, 2022, 7:32pm 1

Download file:
NPC chase test.rbxl (34.7 KB)

The code will loop through the players and check who is the closest distance away from the NPC. The NPC will chase the closest player using the pathfinding service (made to work in a maze) I’m new to scripting and I’m wondering if there’s a better way to write the code.

local NPC = workspace.NPC NPC.PrimaryPart:SetNetworkOwner(nil) local pfs = game:GetService("PathfindingService") -- Pathfinding service local runService = game:GetService("RunService") local hum = script.Parent:WaitForChild("Humanoid") -- npc humanoid local torso = script.Parent:WaitForChild("Torso") -- npc torso local players = game:GetService("Players") local distance local closestPlayer, closestDistance = nil, math.huge function checkPlayerDistance() -- Function will loop through the players and get the nearest player local children = players:GetChildren() for i,v in pairs(children) do if v then distance = (v.Character:FindFirstChild("HumanoidRootPart").Position - torso.Position).magnitude if distance <= closestDistance then closestDistance = distance closestPlayer = v end end end return closestPlayer end runService.Heartbeat:Connect(function() -- NPC will follow the nearest player local returnedPlayer = checkPlayerDistance() local path = pfs:CreatePath() path:ComputeAsync(torso.Position, returnedPlayer.Character.HumanoidRootPart.Position) local waypoints = path:GetWaypoints() local function npcStuck() local pos1 = torso.Position wait(1) local pos2 = torso.Position if (pos1 - pos2).magnitude < 1 then end end if waypoints and waypoints[2] then local pos = waypoints[2] hum:MoveTo(pos.Position) end closestDistance = math.huge end)

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-08-11 20:08 最后登录:2025-08-11 20:08
栏目列表
推荐内容