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

How do I make a brick go to a random position on the baseplate?

So my baseplate has the size: 100.65, 11.744, 100.65

Now I want a brick to spawn somewhere on the baseplate. But I think im mesing something up with the math.random() function. The following code is supposed to spawn the bricks at a random position on the baseplate as I already said. But they fly off the map.

while wait(0.4) do
	local Meteor = game.Lighting.Meteor:Clone()
	local PosX = math.random(0,100)
	local PosY = 130
	local PosZ = math.random(0,100)
	
	Meteor.Parent = game.Workspace
	Meteor.Position = Vector3.new(PosX, PosY, PosZ)
end

So I made it that pos x and z change since pos y needs to be the same hight.

2 comments

0

math.random isn't the problem, you can check with using print

0

How?

1 answer

✓ Accepted answer

Hi SkullMasterHD. I will be helping you regarding the question, "How do I make a brick go to a random position on the baseplate?". Before giving you the code, here are few things you could do for next time.

  1. Lightning is not used for storage. Use ReplicatedStorage or ServerStorage
  1. You will notice that when you clone your model, it will only clone once.

Take a look on how I solved your problem.

-- Declaration Section 

local meteor = game:GetService("ReplicatedStorage"):WaitForChild("Meteor")

-- Processing Section 

while true do
	local xPosition = math.random(100)
	local yPosition = 130
	local zPosition = math.random(100)		
	
	print ("The meteor location is, " .. xPosition.. ", ".. yPosition .. ", ".. zPosition)
	
	local newMeteor = meteor:Clone()
	print ("Cloning meteor")
	newMeteor.Parent = workspace
	newMeteor.Position = Vector3.new(xPosition, yPosition, zPosition)
	print ("Added meteor")
	wait(0.5)
	print ("Redoing work")
end

Let me know if there are any errors. If helped, mark it as answered. Have a lovely day of coding!

2 comments

0

So one thing I would maybe correct you is the meteors are cloning properly on 0.4 so it doesnt clone once. But the only thing that I needed fixed is the random positition since the meteors sometimes spawn outside the map and sometimes on the baseplate if you know what I mean.

0

^ Then set the maximum position of the random number to the maximum size of the baseplate. Isn't that all?

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