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

How to make an npc spawn&despawn at a certain place?

Asked by 2 years ago

Can someone tell me how to? im trying to make a scary obby

0
put the model in a variable then clone it, parent it to workspace to spawn and destroy to despawn greatneil80 2647 — 2y

1 answer

Log in to vote
0
Answered by
GShocked 150
2 years ago
Edited 1 year ago

Insert your own NPC model and use it in the script. Adjust the spawning and despawning as needed.

-- Create the NPC
local npc = Instance.new("Model")
npc.Name = "Scary Gorilla"
npc.Parent = game.Workspace

-- Set the initial position of the NPC
local initialPosition = Vector3.new(0, 0, 0)
npc.PrimaryPart.Position = initialPosition

-- Create a humanoid for the NPC
local humanoid = Instance.new("Humanoid")
humanoid.Parent = npc

-- Spawn the NPC after a delay
wait(5)
npc:SetPrimaryPartCFrame(initialPosition)

-- Make the NPC walk towards players
while true do
    task.wait()
    local closestPlayer = game.Players:GetClosestPlayer(npc.PrimaryPart.Position)
    if closestPlayer then
        -- Make the NPC's humanoid walk towards the player's character
        humanoid:MoveTo(closestPlayer.Character.PrimaryPart.Position)

        -- Despawn the NPC after a delay
        wait(10)
        npc:Destroy()
    end
end
0
Hello, thank you for your answer, but can you explain the instial part? I happen to have errors with it, and I don't know how to fix them. XSadVibesPeopleX 0 — 1y
Ad

Answer this question