Positioning random parts

How do I make a part spawn at a random position on the baseplate? Kind of like a coin spawner. Thanks

You can utilize math.random to create a set of coordinates that the parts will be placed at. Here’s a basic example:

local function placeParts(numberOfParts)

    for count = 1, numberOfParts do -- This will loop through the code for the "numberOfParts" (e.g., sending 5 through means this will be looped through 5 times)

        local randomX = math.random(0, 100) -- Allows the part to spawn in an X coordinate between 0 and 100
        local randomY = math.random(5, 10) -- Allows the part to spawn in a Y coordinate between 5 and 10
        local randomZ = math.random(-100, 100) -- Allows the part to spawn in an Z coordinate between -100 and 100

        local newPart = Instance.new("Part")
        newPart.Name = ("Part #"..count)
        newPart.Anchored = true
        newPart.Position = Vector3.new(randomX, randomY, randomZ) -- Places the part in a random position from the math.random values which were generated
        newPart.BrickColor = BrickColor.new(randomX, randomY, randomZ)
        newPart.Parent = workspace

        warn(newPart.Name.."'s coordinates are "..tostring(newPart.Position))
    end
end

placeParts(5) -- This will call the function and create that many parts
1 Like

I never tried this, but

You can make a variable and do

local random = math.random(1,10)

and then do a if statement to check if random == 1 then do cframe = cframe.new(position)

so:

if random == 1 then
script.Parent.CFrame = CFrame.new(position)
and add elseif statements to see if its a different number

My baseplate position is {197.78, -2.23, 205.857} so do I do it like this?

local randomX = math.random(-197.78, 197.78)
local randomY = math.random(5, 10)
local randomZ = math.random(-205.857, 205.857)

Server or Client script


local replicatedStorage = game:GetService("ReplicatedStorage")
local item = replicatedStorage.Part -- Put whereever the coin is in here
local baseplate = workspace.Baseplate --Put whatever is the baseplate in here
local rnd = math.random

function DropItems(amount, height)
	local baseplatePos = baseplate.Position -- positrion of the baseplate
	local baseplateSizeX = baseplate.Size.X / 2 --offset
	local baseplateSizeZ = baseplate.Size.Z / 2 --offset
	local baseplateSizeY = baseplate.Size.Y / 2 --offset
	
	for i = 1,amount do
		local newCoin = item:Clone()
		newCoin.Parent = workspace -- put whereever you want to the coin to go in here.
		
		local baseplateFinalPosX = baseplatePos.X + (rnd(-baseplateSizeX,baseplateSizeX)) --position on x coord
		local baseplateFinalPosY = baseplatePos.Y + height + (newCoin.Size.Y / 2) -- positon on y coord
		local baseplateFinalPosZ = baseplatePos.Z + (rnd(-baseplateSizeX,baseplateSizeZ)) --position on z coord
		
		local generateRndPos = Vector3.new(baseplateFinalPosX,baseplateFinalPosY,baseplateFinalPosZ) --makes pos
		newCoin.Position = generateRndPos --applies
	end
end


--Replace this with whatever you will use to call the funation

DropItems(25,0)
-- The first num(25) is how many to spawn
-- The second is the height off the ground to spawn them

You can use this function to place a part at a random position on a baseplate. You will probably need to edit it to change where you get the stored coin from, and where the new coin goes. This function will allow you to pick the height and number of items spawned. 0 height will mean that there is 0 studs from the bottom of the item to the baseplate. Keep in mind this will only property work with a flat surface, if you wanted to do this will hilly areas I would suggest using Raycasting to generate the Y positon. Any questions, feel free to ask.

6 Likes

Here is the one with the Raycast for the uneven terrain. This will ignore players. (Updated)

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local item = replicatedStorage.Part -- Put whereever the coin is in here
local baseplate = workspace.Baseplate --Put whatever is the baseplate in here
local rnd = math.random

function DropItems(amount, height)
	local baseplatePos = baseplate.Position -- positrion of the baseplate
	local baseplateSizeX = baseplate.Size.X / 2 --offset
	local baseplateSizeZ = baseplate.Size.Z / 2 --offset
	local baseplateSizeY = baseplate.Size.Y / 2 --offset

	for i = 1,amount do
		local newCoin = item:Clone()
		newCoin.Parent = workspace -- put whereever you want to the coin to go in here.

		local baseplateFinalPosX = baseplatePos.X + (rnd(-baseplateSizeX,baseplateSizeX)) --position on x coord
		local baseplateFinalPosZ = baseplatePos.Z + (rnd(-baseplateSizeX,baseplateSizeZ)) --position on z coord

		local baseY = baseplatePos.Y
		local newPos = Vector3.new(baseplateFinalPosX, baseY + 1000, baseplateFinalPosZ)
		local downPos = Vector3.new(baseplateFinalPosX, baseY - 1000, baseplateFinalPosZ)
		local igList = {}

		for _,x in pairs(players:GetChildren()) do -- block players as hit
			local char = x.Character or x.CharacterAdded:Wait()
			table.insert(igList, char)
		end

		local params = RaycastParams.new()
		params.FilterType = Enum.RaycastFilterType.Blacklist
		params.FilterDescendantsInstances = igList

		local result = workspace:Raycast(newPos, CFrame.new(newPos, downPos).LookVector * 10000, params)
		print(result)

		if result ~= nil then
			local baseplateFinalPosY = result.Position.Y + height + (newCoin.Size.Y / 2) -- positon on y coord

			local generateRndPos = Vector3.new(baseplateFinalPosX,baseplateFinalPosY,baseplateFinalPosZ) --makes pos
			newCoin.Position = generateRndPos --applies
		else
			newCoin:Destory()
			warn("Coin was destroyed because of invalid placement position")
		end
	end
end


--Replace this with whatever you will use to call the funation

DropItems(25,0)
-- The first num(25) is how many to spawn
-- The second is the height off the ground to spawn them
2 Likes

:FindPartOnRayWithIgnoreList is deprecated. WorldRoot | Roblox Creator Documentation

2 Likes

Fixed it, I was a little confused about whether to use :Raycast() or :findPartOnRayWithIgnoreList(), when I was learning Raycasting I was taught to do it using :findPartOnRayWithIgnoreList(), not :Raycast(). But now i’ve seen people use both I know how to use both I just used :findPartOnRayWithIgnoreList(), but thanks for clearing it up! I updated the Code to be using :Raycast().

2 Likes