Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I find the nearest character?

Asked by 5 years ago
Edited 5 years ago

I am trying to use path finding for a custom enemy. I have the basic path finding script done but I don't know how to locate the nearest player. This is my first time using path finding so I am not very confident about the script I made. The script only locates the object when the game starts up and if i move the object, the enemy does not change its path. This is overall confusing to me and I desperately need help. Here is the script i'm using (I got it off the Roblox wiki.)

local PathfindingService = game:GetService("PathfindingService")

-- Variables for the zombie, its humanoid, and destination

local zombie = script.Parent

local humanoid = zombie.Humanoid

local destination = game.Workspace.End

-- Create the path object

local path = PathfindingService:CreatePath()

-- Variables to store waypoints table and zombie's current waypoint

local waypoints

local currentWaypointIndex

local function followPath(destinationObject)

-- Compute and check the path

path:ComputeAsync(zombie.HumanoidRootPart.Position, destinationObject.PrimaryPart.Position)

-- Empty waypoints table after each new path computation

waypoints = {}

if path.Status == Enum.PathStatus.Success then

-- Get the path waypoints and start zombie walking

waypoints = path:GetWaypoints()

-- Move to first waypoint

currentWaypointIndex = 1

humanoid:MoveTo(waypoints[currentWaypointIndex].Position)

else

-- Error (path not found); stop humanoid

humanoid:MoveTo(zombie.HumanoidRootPart.Position)

end

end

local function onWaypointReached(reached)

if reached and currentWaypointIndex < #waypoints then

currentWaypointIndex = currentWaypointIndex + 1

humanoid:MoveTo(waypoints[currentWaypointIndex].Position)

end

end

local function onPathBlocked(blockedWaypointIndex)

-- Check if the obstacle is further down the path

if blockedWaypointIndex > currentWaypointIndex then

-- Call function to re-compute the path

followPath(destination)

end

end

-- Connect 'Blocked' event to the 'onPathBlocked' function

path.Blocked:Connect(onPathBlocked)

-- Connect 'MoveToFinished' event to the 'onWaypointReached' function

humanoid.MoveToFinished:Connect(onWaypointReached)

followPath(destination)

0
I don't think the script was imputed properly. The new update is confusing. OneDelectableFruit 7 — 5y
0
magnitude DeceptiveCaster 3761 — 5y
0
u need to tab each line to format it EXpodo1234ALT 18 — 5y
0
holy crap I TOLD YOUTHIS POOPY FACE EDITOR IS POOPY!!! Fifkee 2017 — 5y

1 answer

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago
Edited 5 years ago

local MainPart = script.Parent:WaitForChild('HumanoidRootPart') function getNearestRootPart(maxRadius) local closestRootPart = nil; --for returning local closestRootPartNumber = nil; --for comparisons for i, v in pairs(game.Players:GetPlayers()) do local Character = v.Character -- or v.CharacterAdded:wait(), don't use CharacterAdded:wait() if you need to do something important if (Character:FindFirstChild('HumanoidRootPart')) then if (not closestRootPart) then if (math.abs((MainPart.Position-closestRootPart.Position).magnitude)) <=maxRadius then closestRootPart = Character.HumanoidRootPart; closestRootPartNumber = math.abs((MainPart.Position-closestRootPart.Position).magnitude) end else --I don't check if it's under the radius because the first rootpart must be under the radius in order for it to be detected, so I'll instead check if the next rootpart is closer. if math.abs((Character.HumanoidRootPart.Position-MainPart.Position).magnitude) <= currentRootPartNumber then closestRootPart = Character.HumanoidRootPart; closestRootPartNumber = math.abs((Character.HumanoidRootPart.Position-MainPart.Position).magnitude); end end return closestRootPart end --i gave you the full answer since I was lazy lol, it's 01:37 in the morning and I'm supposed to be working on hw --also, i'm missing a few ends somewhere, but I don't know where! --looks like you gotta do some work! ;( --(this was on purpose please don't negrep me i love you) --if something is wrong with the script that doesn't relate to using "end" then please tell me in comments ily --holy crap 1406 characters and 1660 bytes
0
GETTING STACKEDIT WAS A MISTAKE WHY DO THEY ALLOW ```lua print('a')``` BUT IT DOESNT FORMAT WELL, I HAD TO USE ~~~ INSTEAD AAAA Fifkee 2017 — 5y
Ad

Answer this question