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

How do I make a model spawn at different spots?

Hi, I'm trying to make a model spawn at different spots in a place and I want people to catch the model and earn a badge. How do I do that? and also I'm new to scripting.

2 answers

Let's assume you have a model aptly named "Model" in game.ServerStorage

Your spawning script should look like this:

--Initiate the model variable
model = game.ServerStorage:FindFirstChild("Model")

--Spawning function
function spawnmodel(modeltospawn)
	--Randomize numbers (optional)
	math.randomseed(tick())
	--Create a clone of the model
	local modelclone = modeltospawn:Clone()
	--Choose the location where the model spawns. Further explanation below.
	local modelpos = CFrame.new(math.random(x1,x2), <height>, math.random(y1,y2)
	--Set the position of the clone to the randomly selected position
	modelclone.Position = modelpos
	--Move the clone to game.Workspace (makes it appear in the game)
	modelclone.Parent = game.Workspace
	modelclone:MakeJoints()
	--Wait until the model is gone before allowing anything else to run
	while game.Workspace.model do
		wait()
	end
end

while true do
	math.randomseed(tick())
	--Wait between 5 and 20 seconds
	wait(math.random(5,20)
	spawnmodel()
end

Now, the model position picker on line 11.

How it works is it takes a number between x1 and x2 and makes that the model's x position, and takes a number between y1 and y2 and makes that the model's y position. This is important if we want our model to only spawn in a certain area. The height here is constant.

An easy way to find the numbers you need is to place 2 bricks at the corners of the area you want. When you look at the position property of the brick you'll see 3 numbers. Copy down the first one off the first brick and the first one off the second one and put them into the "math.random(x1,x2)" part. Do the same with (y1, y2), but with the last numbers of the position. To set the height, place another part where you want the model to be and copy down the middle number. Once you have all your position values, you can then remove the parts you used to find the numbers.

1 comment

1

this is rlly nice of u to do...

Use the function :SetPrimaryPartCFrame() like this:

model:SetPrimaryPartCFrame(CFrame.new(math.random(workspace.Baseplate.CFrame.X, workspace.Baseplate.CFrame.X+workspace.Baseplate.Size.X, workspace.Baseplate.CFrame.Y, workspace.Baseplate.CFrame.Z, workspace.Baseplate.CFrame.Z+workspace.Baseplate.Size.Z)

The function :SetPrimaryPartCFrame() requires a model to have a PrimaryPart set. It will change the CFrame of the Model's PrimaryPart and any other Part inside of it aswell.(Parts in Tools, Hats, Models, etc. included)

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