Ask AI on The Internet
Question: Write a roblox sudio script that a npc follows you and kill you but if you go inside of a "par" then the npc walks away and stop killing you until you leave the block . Because i am noob pls tell me how to do it step by step .
Here's a step-by-step guide to create a Roblox Studio Script for an NPC that follows the player, kills them, and stops when the player enters the "par" block: 1. Open Roblox Studio. 2. Create a new Baseplate or open an existing game where you want to add the NPC. 3. Insert an NPC character or model into the game. You can find pre-built NPC characters in the Toolbox, or you can create your own custom NPC. 4. Name your NPC. For this example, we will name it "KillerNPC". 5. Create a new part in the workspace and name it "par". This will be the safe area where the NPC stops chasing the player. 6. Make sure both the NPC and the "par" Part are located in the workspace. 7. To make the script, right-click on the "KillerNPC", hover over "Insert Object", and then click on "Script". This will create a new script inside the NPC. 8. Now, let's start coding inside the script. Remove the default "Hello, world!" code and write the following script: ```lua local npc= script.Parent local human = npc:FindFirstChild("Humanoid") local par = workspace:FindFirstChild("par") local function findNearestPlayer() local nearestPlayer = nil local minDistance = math.huge for _, player in ipairs(game.Players:GetPlayers()) do local character = player.Character if character and character:FindFirstChild("HumanoidRootPart") then local distance = (character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude if distance < minDistance then nearestPlayer = character minDistance = distance end end end return nearestPlayer end local function inSafeZone(player) local distance = (player.HumanoidRootPart.Position - par.Position).Magnitude return distance < par.Size.X / 2 end while true do local target = findNearestPlayer() if target and human and not inSafeZone(target) then human:MoveTo(target.HumanoidRootPart.Position) human:ChangeState(Enum.HumanoidStateType.Running) if (target.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude < 5 then target.Humanoid.Health = 0 end else human:ChangeState(Enum.HumanoidStateType.Idling) end wait(.1) end ``` This script does the following: - Defines the related variables for the NPC, the humanoid component of the NPC, and the "par" part. - The `findNearestPlayer()` function checks for the nearest player in the game. - The `inSafeZone()` function checks if the player is in the "par" part by measuring the distance between the player and the part's center. - The while loop works continuously to update the NPC's behavior depending on the player's location. 9. Save your game, close the script editor, and test the game by clicking "Play". The NPC should follow and kill players when they are outside the "par" block, and it should stop chasing players once they enter the "par" block. 10. Feel free to adjust the NPC and "par" part's properties or the script for your specific requirements.
A new large language model has been made available since this question was asked.
To generate an updated answer from the new model, click the button below.
Think your answer is better than the AI's? Post it below.
Other Most Relevant Posts:
If you want your question answered by an AI, click here.
Post your own comment: