How do I make a script that spawns something at a random time?

I want to add little random events that spawn at random times to my game to boost engagement.
What I’m looking for is pretty much a way to randomly decide to make a part visible and turn on the collision.
But if there’s another better way to do this that would also be helpful!
For clarification, the parts are all a model and I want them to spawn in the same spot every time. Sorry for not clarifying from the start.

1 Like

Use math.random(). For example:

local part = script.Parent

while true do
task.wait(math.random(1, 10)) -- this waits a random interval between 1-10 seconds
part.Transparency = 1
task.wait(math.random(1, 10))
part.Transparency = 0
end
1 Like

It depends on what you want to do with these parts. You would definitely be using math.random for a good portion of it. I’m assuming you’ll be wanting to place them in specific random areas, rather than completely random, that way they won’t spawn underground or off the map. In which case, you should create “spawn blocks” that are invisible and have them in a folder in your workspace called “EventSpawners”. Then, every so often, you can choose one of them (or more!) to spawn your object into. Here’s an example snippet:

local spawners = game.workspace.EventSpawners
local eventObject = game:GetService:ReplicatedStorage() //<- reference your event model you want here
local minimumInterval = 5 //<- shortest time itll wait
local maximumInterval = 15  //<- longest time

function spawnEvent()
   local chosenSpawnIndex = math.random(1, #spawners:GetChildren()) //<- randomly selects one of the spawners in the folder
   local chosenSpawner = spawners:GetChildren()[chosenSpawnIndex]
   local newEvent = eventObject:Clone()
   newEvent:PivotTo(CFrame.new(chosenSpawn.Position)) //<- this is assuming the new event is a model with a primary part
   newEvent.Parent = workspace
end

while true do 
   task.wait(math.random(minimumInterval, maximumInterval)
   spawnEvent()
end

There are a lot of different ways to implement this.

This script uses the CollectionService so any parts with the tag “RandomCollection” will be hidden or shown every second with even probability. It could be saved in a Script in ServerScriptService or a LocalScript in ReplicatedFirst to run on the client instead. The Threshold variable controls the odds of a part being hidden. Closer to 1 parts will be hidden more often. Closer to 0 parts will be visible more often.

This works with simple parts. With models you’d need to customize the hide/show routine to change the properties of the model’s descendants.

local CollectionService = game:GetService("CollectionService")

local Tag = "RandomCollection"
local Threshold = 0.5
local Debug = false

local Instances = {}

print("Initializing RandomCollection")

local function instanceadded(part)
	if part:IsA("BasePart") then
		table.insert(Instances,part)
		if Debug then
			print("Adding",part)
		end
	end
end
local function instanceremoved(part)
	local dex = table.find(Instances,part)
	if dex then
		table.remove(Instances,dex)
		if Debug then
			print("Removing",part)
		end
	end
end
CollectionService:GetInstanceAddedSignal(Tag):Connect(instanceadded)
CollectionService:GetInstanceRemovedSignal(Tag):Connect(instanceremoved)
for _,part in CollectionService:GetTagged(Tag) do
	instanceadded(part)
end

while task.wait(1) do
	for _,part in Instances do
		if math.random() > Threshold then
			part.Transparency = 1
			part.CanCollide = false
			if Debug then
				print("Hiding",part)
			end
		else
			part.Transparency = 0
			part.CanCollide = true
			if Debug then
				print("Showing",part)
			end
		end
	end
end