Can you make a roblox script for a npc? that follows the player if in range
- User
Can you make a roblox script for a npc? that follows the player if in range
Assistant
Roblox NPC Follow Script
Here's a script that makes an NPC follow a player when they're within a certain range:
-- Place this script inside your NPC model (preferably in a Script object) local npc = script.Parent -- The NPC model local humanoid = npc:WaitForChild("Humanoid") local rootPart = npc:WaitForChild("HumanoidRootPart") -- Configuration local followRange = 20 -- Distance in studs to start following local stopRange = 5 -- Distance in studs to stop following local followSpeed = 16 -- Walking speed when following local idleSpeed = 0 -- Speed when not following local updateInterval = 0.5 -- How often to check distance (seconds) -- Variables local currentTarget = nil local isFollowing = false -- Function to find the nearest player local function findNearestPlayer() local nearestPlayer = nil local shortestDistance = followRange for _, player in ipairs(game.Players:GetPlayers()) do if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then local distance = (player.Character.HumanoidRootPart.Position - rootPart.Position).Magnitude if distance < shortestDistance then shortestDistance = distance nearestPlayer = player end end end return nearestPlayer end -- Function to start following local function startFollowing(player) if isFollowing then return end isFollowing = true currentTarget = player humanoid.WalkSpeed = followSpeed -- Connect to player's character being removed local disconnectFunction = player.CharacterRemoving:Connect(function() stopFollowing() end) -- Store the disconnect function to call it later npc:SetAttribute("FollowDisconnect", disconnectFunction) end -- Function to stop following local function stopFollowing() if not isFollowing then return end isFollowing = false humanoid.WalkSpeed = idleSpeed currentTarget = nil -- Disconnect the character removing event local disconnectFunction = npc:GetAttribute("FollowDisconnect") if disconnectFunction then disconnectFunction:Disconnect() npc:SetAttribute("FollowDisconnect", nil) end end -- Main follow loop while true do if not npc or not npc.Parent or not humanoid or humanoid.Health <= 0 then break -- NPC is destroyed or dead end if isFollowing then -- Check if we should stop following if currentTarget and currentTarget.Character and currentTarget.Character:FindFirstChild("HumanoidRootPart") then local distance = (currentTarget.Character.HumanoidRootPart.Position - rootPart.Position).Magnitude if distance > followRange then stopFollowing() elseif distance <= stopRange then -- Stop moving when close enough humanoid:MoveTo(rootPart.Position) else -- Continue following humanoid:MoveTo(currentTarget.Character.HumanoidRootPart.Position) end else stopFollowing() end else -- Check if we should start following local nearestPlayer = findNearestPlayer() if nearestPlayer then startFollowing(nearestPlayer) end end wait(updateInterval) endHow to Use This Script:
-
Create an NPC model in Roblox Studio with:
- A Humanoid object
- A HumanoidRootPart
- Any other parts/meshes you want for your NPC
-
Insert a Script object into your NPC model
-
Paste this code into the Script
-
Adjust the configuration variables at the top to change:
followRange: How close a player needs to be to start followingstopRange: How close the NPC will get to the playerfollowSpeed: How fast the NPC moves when followingidleSpeed: How fast the NPC moves when not following (usually 0)updateInterval: How often the NPC checks for players (lower = more responsive but less efficient)
Additional Features You Could Add:
- Pathfinding for better navigation around obstacles
- Different animations for walking/idle states
- Sound effects when the NPC starts following
- Visual indicators when the NPC is following
- Different behaviors based on player actions
Would you like me to modify this script in any particular way?
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.