Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local RunService = game:GetService("RunService")
- local LocalPlayer = Players.LocalPlayer
- -- Function to find the nearest NPC (as provided)
- local function findNearestNPC()
- local nearestNPC = nil
- local nearestDistance = math.huge
- local Character = LocalPlayer.Character
- if not Character or not Character:FindFirstChild("HumanoidRootPart") then return end
- local dungeon = workspace:FindFirstChild("dungeon")
- if not dungeon then return end
- for _, child in ipairs(dungeon:GetChildren()) do
- if child:FindFirstChild("enemyFolder") then
- for _, npc in ipairs(child.enemyFolder:GetChildren()) do
- if npc:FindFirstChild("HumanoidRootPart") and npc:FindFirstChild("Humanoid") and npc.Humanoid.Health > 0 then
- local distance = (Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude
- if distance < nearestDistance then
- nearestDistance = distance
- nearestNPC = npc
- end
- end
- end
- end
- end
- return nearestNPC
- end
- local function freezeCharacter(character)
- if character and character:FindFirstChild("HumanoidRootPart") then
- character.HumanoidRootPart.Anchored = true
- end
- end
- local function teleportToNPC()
- local Character = LocalPlayer.Character
- if not Character or not Character:FindFirstChild("HumanoidRootPart") then return end
- local npc = findNearestNPC()
- if npc and npc:FindFirstChild("HumanoidRootPart") then
- local npcPosition = npc.HumanoidRootPart.Position
- local teleportPosition = Vector3.new(npcPosition.X, npcPosition.Y + 12, npcPosition.Z)
- -- Calculate the direction to face the NPC
- local lookVector = (npcPosition - teleportPosition).Unit
- local lookCFrame = CFrame.new(teleportPosition, teleportPosition + lookVector)
- Character.HumanoidRootPart.CFrame = lookCFrame
- freezeCharacter(Character)
- end
- end
- -- Main loop
- local function mainLoop()
- while true do
- teleportToNPC()
- wait(2) -- Wait for 2 seconds before the next teleportation
- end
- end
- -- Function to setup character
- local function setupCharacter(character)
- character:WaitForChild("HumanoidRootPart")
- freezeCharacter(character)
- coroutine.wrap(mainLoop)()
- end
- -- Initial setup
- if LocalPlayer.Character then
- setupCharacter(LocalPlayer.Character)
- end
- -- Handle character respawns
- LocalPlayer.CharacterAdded:Connect(setupCharacter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement