How do i make parts spawn randomly over a map?

I am new to roblox scripting and I am making a simulator, It’s about wood cutting and I want to make trees spawn randomly over the map, and stop at some point so the map isn’t overwhelmed by trees

1 Like

You could set the part position to something like this:

Vector3.new(math.random(0,120), math.random(0, 90), math.random(0, 10))

1 Like

Firstly, will use 2 parts to define the region you want your wood to spawn in, and save their locations. Something like this:
Part1 = -93, 15.5, 120
Part2 = 132, 15.5, -34

Put your wood object into ReplicatedStorage.
Insert the script as follows:

game.ReplicatedStorage.Wood:Clone()
game.ReplicatedStorage.Wood.Parent = workspace
wait(2) -- Add a wait to let the object parent properly

game.Workspace.Wood.Position = Vector3.new(math.random(-93, 132), 15.5, math.random(120, -34)) -- Replace these math.random with your Part1 and Part 2 X position, your Part1 and Part2 Y position and so on.

I think that’s it. Let me know if there’s anything wrong with it. :slight_smile:

6 Likes

sorry but how do i ‘define region’ ? also thanks for reply

1 Like

Let’s say you have a baseplate as your map. Insert one part in the one corner and the other one in the exact opposite like:

Part1 . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . Part2

Then go to properties, and write down their location.
This will allow you to create a region for the wood to spawn in.

4 Likes

thanks, but could you please tell me if i can put more than 2 parts because i have an area in center of the map where i dont want trees?

Hello again! Make a transparent part where you don’t want trees spawning in. Make sure it’s CanCollide off so people can walk through it. Insert the script in the transparent part:

script.Parent.Touched:Connect(function(otherPart)
if otherPart.Name == "Wood" then
otherPart:Destroy() -- I'm not sure if this block works.
print("Wood removed from spawn area.")
end
end)
1 Like

You could even GetTouchingParts() then check every single name of the object in the table and check if its called Wood then destroy it

1 Like

ah i finally understand, i’ll try it now thanks

1 Like

A small FYI, this topic is to correct scripts, not create ones for you. It’s listed on the topic rules. So next time, please post any previous attempts you’ve made on creating this code.

2 Likes

oh alright sorry i just recently got promoted so i didnt know

1 Like

also i have one more question, do i need to name my wood “wood” or no?

Yes. Else, you will need to re-define it using its name

1 Like

sorry if i am asking too much but i cannot figure out where do i put the first 2 scripts this; Part1 = x Part2 = x and script: game.ReplicatedStorage.Wood:Clone()
game.ReplicatedStorage.Wood.Parent = workspace
wait(2) – Add a wait to let the object parent properly
game.Workspace.Wood.Position = Vector3.new(math.random(-93, 132), 15.5,
math.random(120, -34))

bro i just checked it and idk if i did something wrong but its not working at all @Kostiskat

Hello! Kostikat actually provided the correct answer, and I’ll explain how you would do that:

Firstly, since you want to have a region you would have 2 parts which would define the region positions.
The two parts would be in the opposite corners, and you’d get the X and Z position of them and put them into a variable.

local aX = PointA.Position.X
local bX = PointB.Position.X
local aZ = PointA.Position.Z
local bZ = PointB.Position.Z

For the Y position you can also do the same or simply put a static number if you want the object in the same height.

You will use math.random to get a number between those two, which is necessary if you don’t want the object to go off the region you defined!

The position property requires 3 arguments: X,Y,Z. Through Vector3 you you’d set those 3 arguments:

(math.random(bX, aX), (TheYValue), math.random(bZ, aZ)

Now you have the random position for the wood you’re going to place! Which is inside the region you’ve defined, since you’ve used math.random to get a number between the position variables.

Now you only have to clone the wood, .Parent it to workspace and set It’s position to that number!

Here’s a quick video I made to demonstrate this:

On this video I used that exact idea on a function that triggers everytime I click that button! You see It’s working perfectly as intended. The red brick is PointB and the blue one is PointA.

It’s actually a very short and simple script, take a look:

local Button = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local PointA = game.Workspace.Area:WaitForChild("PointA")
local PointB = game.Workspace.Area:WaitForChild("PointB")
local Wood = ReplicatedStorage:FindFirstChild("Wood")

Button.MouseClick:Connect(function()
	local aX = PointA.Position.X
	local bX = PointB.Position.X
	local aZ = PointA.Position.Z
	local bZ = PointB.Position.Z
	
	local ClonedWood = Wood:Clone()
	ClonedWood.Parent = workspace
	ClonedWood.Position = Vector3.new(math.random(bX, aX), 2.15, math.random(bZ, aZ))
end)

I don’t know how’s your gameplay, so this might not be the exact solution for your game!
Instead of using this raw script, you can use this simple idea I’ve provided and make scripts that suits your game the correct way or even more complex scripts!

Hope this helps!

10 Likes

i very greatly appreciate your effort trying to explain that, but i cannot try that out right now since im battling a huge problem against my scripts not working (like they’re deleted after running a game in studio. if you have any clue about it, please let me know.

also im really sorry but this is not the thing i need, lets take this as a coins in some simulator, they randomly appear in specific area but it does not overflow with them, again really sorry.

You’ve probably inserted a backdoor in your game, which is intentionally destroying every single script in your game. That can happen through free models and plugins.

I suggest reading this thread: How to remove backdoors from your game
Also for the Coins simulator, I suggest reading this thread: Creating a Coin Collection System

For future topics please read over the devforum, such as on the #resources:community-tutorials, there’s probably some solution over there!

1 Like