Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

1 comment

0

put the model in a variable then clone it, parent it to workspace to spawn and destroy to despawn

1 answer

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

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.