How to make a random thing spawner and despawn

i want this to spawn randomly in map and despawn randomly in the map automatically… What script can i add or what can i do to make it do the function…?

2 Likes

What is it that you’re spawning? Is it a part or a model?

it’s a model made up of parts?

What you gotta do is put a bunch of invisible parts inside a map. Put them all in a folder called spawns, and then put this script in ServerScriptService:

local spawns = game.Workspace.spawns:GetChildren()
local RandomSpawn = math.random(1, #spawns
local Players = game.Players:GetChildren()

for I,v in pairs(Players) do
    local char = v.Character or v.CharacterAdded:Wait()
    char.PrimaryPart.CFrame = RandomSpawn.CFrame + Vector3.new(0,5,0)
end

with this would they despawn too? for them spawning again with a time duration…

No there wouldn’t be a despawn in that script, but you can copy that code but just change the destination parts to the parts in the spawn area/lobby. The time duration could be an Intermissions

local model = workspace.Model
local folder = workspace.Spawns

for i = 1,30 do -- how many times you want it to clone
	local clone = model:Clone()
	clone:PivotTo(CFrame.new(Random.new():NextInteger(-300, 300), Random.new():NextInteger(-300, 300), Random.new():NextInteger(-300, 300)))
	clone.Parent = folder
end

task.wait(10) -- how long before despawn

for _,v: Model in ipairs(folder:GetChildren()) do
	if v:IsA("Model") then
		v:Destroy()
	end
end

If you are spawning the same item at different places on the map, then you would have the following:

  • A list of spawn locations
  • The item to spawn a folder in ServerStorage

When it’s time to spawn the item, just set the position of the item (PrimaryPartCFrame if it’s a model) and parent it to the workspace. When it’s time to despawn, just reparent it back to your folder in ServerStorage. If you are spawning multiple items, then you need a copy of each item for how many concurrent spawns you plan on having.

Doing it this way will save memory and help reduce lag on the server because you’re not having the same copy of the parts/models scattered all over the map.

local serverStorage = game.GetService("ServerStorage")
local spawnFolder = serverStorage.SpawnFolder
local spawnItem = spawnFolder:FindFirstChild("SpawnItem")
local spawnPos = {}
local rand = Random.new()

-- Spawn Locations
table.insert(spawnPos, Vector3.new(x1, y1, z1))
table.insert(spawnPos, Vector3.new(x2, y2, z2))
table.insert(spawnPos, Vector3.new(x3, y3, z3))
-- etc...
-- Replace x, y, z with your actual coordinates.

while true do
	local waitTime = task.wait(rand.NextNumber(15, 30))
	local pos = spawnPos[rand.NextInteger(1, #spawnPos)]
	if spawnItem:IsA("BasePart") then
		-- BasePart which includes Part and MeshPart
		spawnItem.Position = pos
	elseif spawnItem:IsA("Model") then
		spawnItem.PrimaryPart.CFrame = CFrame.new(pos)
	else
		-- XXX: Default that may not work.
		spawnItem.Position = pos
	end
	spawnItem.Parent = game.Workspace
	delay(rand:NextNumber(5, waitTime - 0.5), function()
		spawnItem.Parent = spawnFolder
	end)
end

Put this in your ServerScriptService. Parts will spawn every 15-30 seconds and will last 5 to waitTime - 0.5 seconds. This is to ensure that only one part is spawned at a time. This will also handle part types which are descendent of BasePart and Models. If you are using a Model, make sure that you have the primary part specified in the model properties or this will error out.

There might be errors in this since I just wrote this off the top of my head.

2 Likes

Minor nitpick, you should probably use PivotTo() when moving models instead of PrimaryPartCFrame() (unless you give me a reason as to why you shouldn’t).

My understanding of PivotTo() is that it’s a displacement that’s added to the Position field of the CFrame. The position itself doesn’t change at all if you were to read it, but where it gets rendered in the 3D view does change. The OP did not mention what he was doing with this, so I opted for the safer method of just setting the CFrame position.