Help with Random NPC Spawning

I’m making something similar to an SCP demonstration game, but I want the NPC to “spawn” at random positions every time the game is loaded.

Screenshot 2024-02-11 093450

I want the NPC’s position to teleport to one of these spawn parts.

I don’t have a clue on what to do since I’ve actually never did something like this before, any help would be appreciated, thank you.

1 Like

It’s not that difficult once you know a few things

local SCP = where your character is

local Spawns = --go to your spawns here so game.Workspace.Maze.Spawns["SCP Model"]

--so now we need to get the children of the spawns which is just every part

Spawns = Spawns:GetChildren()

--All we have to do now is move the character to a random spawn

SCP:PivotTo(Spawns[math.random(1,#Spawns)].Position)

2 Likes

Yeah, sorry, I’m still pretty new since I don’t code often. But I did get an error on “Unable to cast Instance to Coordinate Frame”.

I forgot about Position my bad. Should work now

It’s easy to code these type of stuff once you have some coding experience.

-- We first get the SCP model, the maze and the spawns.

local SCP = YOUR_MODEL -- Set "YOUR_MODEL" to your npc (for example workspace.SCP)
local Maze = workspace.Maze -- I'm guessing It's located in workspace.
local Spawns = Maze:WaitForChild("Spawns")["SCP Model"]:GetChildren()

-- We then choose a random spawn and set the SCP's PrimaryPart (which is "HumanoidRootPart")
-- and we set that position to one of the spawns.

local ChosenSpawn = Spawns[math.random(1, #Spawns)]
SCP.PrimaryPart.Position = ChosenSpawn.Position

If you have any problems with the code, reply to me.

I tried it, no errors, but the NPC won’t spawn in.

I forgot to set the SCP’s parent to workspace. Oops.
Just replace It with this and it should work fine.

local ChosenSpawn = Spawns[math.random(1, #Spawns)]
SCP.Parent = workspace
SCP.PrimaryPart.Position = ChosenSpawn.Position

if @Haf_p solution doesn’t work for you:

Try this:

local SCP = your model here (ex: game.Workspace.SCP)
local spawnPointName = "SCP Spawn" -- the name of your spawn points
local spawnsFolder = Where your spawns are located

-- function to teleport SCP to a random spawn point
local function teleportSCP()
    local spawnPoints = {}

    -- Find all spawn points with the specified name
    for _, spawnPoint in ipairs(spawnsFolder:GetChildren()) do
        if spawnPoint.Name == spawnPointName then
            table.insert(spawnPoints, spawnPoint.Position)
        end
    end

    if #spawnPoints > 0 then
        local randomIndex = math.random(1, #spawnPoints)
        local randomSpawnPoint = spawnPoints[randomIndex]
        SCP:SetPrimaryPartCFrame(CFrame.new(randomSpawnPoint))
    else
        warn("No spawn points found with the name: " .. spawnPointName)
    end
end

-- Teleport SCP when the game starts
teleportSCP()

this will cycle the folder containing the spawnpoints so you dont have to give a diferent name for each of them

Ensure that:

  • You replace ‘your model here’ (line 1) with the actual reference to the SCP model
  • ‘spawnsFolder’ (line 3) is currently set to the folder where your spawn points are located
  • The names of the spawn points within that folder match “spawnPointName” (line 2)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.