So in the game i’m making I want to spawn npcs around a red part that is the size of where they should spawn randomly.
How can I do this?
So in the game i’m making I want to spawn npcs around a red part that is the size of where they should spawn randomly.
How can I do this?
How are you spawning the NPCs now? There is probably a location or Part used in the script you are using that you can change the value of to make them spawn in a more random area around that spot.
Look up: https://developer.roblox.com/en-us/api-reference/datatype/Random
local npcTemplate = game:GetService('ReplicatedStorage'):WaitForChild('Builderman')
local spawnPart = script.Parent
local random = Random.new()
function getRandomLocationInsidePart(part)
-- Returns the part's CFrame plus or minus half of the part's size
return part.CFrame * CFrame.new(
part.Size * Vector3.new(random:NextNumber(-.5,.5), random:NextNumber(-.5,.5), random:NextNumber(-.5,.5))
)
end
function spawnNPC()
-- Get a random cframe, and use just the position as the spawn point
local randomLocation = getRandomLocationInsidePart(spawnPart).p
-- Get a random rotation so that the npc isn't always facing the same direction
local randomRotation = CFrame.Angles(0,math.pi*random:NextNumber(0,2),0)
local npc = npcTemplate:clone()
local hipHeight = npc.Humanoid.HipHeight
-- Position the NPC at the random location but a little higher because they are standing.
npc.HumanoidRootPart.CFrame = CFrame.new(randomLocation + Vector3.new(0,1+hipHeight,0)) * randomRotation
npc.Parent = workspace
return npc
end
-- Spawn an NPC every 5 seconds, but also remove the old one.
local existingNPC = nil
while true do
if existingNPC then
existingNPC:Destroy()
end
existingNPC = spawnNPC()
wait(5)
end