Random gems spawn

Trying to make a place where gems randomly spawn in a certain area so they don’t spawn inside the mountain or too far from it. How can I achieve this?

9 Likes
local Workspace= game:GetService("Workspace")
local MapSpawmFolder = Workspace:WaitForChild("Maps")
local ServerStorage = game:GetService("ServerStorage")
local Maps = ServerStorage:WitForChild("Maps"):GetChildren()

local randommap = Maps[math.random(1,#Maps)]:Clone()
randommap.Parent = MapSpawmFolder
1 Like

It’s not a map, it is a gem? I am a bit confused. And will the gems spawn in the mountain.?

1 Like

what the code I showed does is it spawns an object(in your case the gem) anywhere inside the area of the part. So you could resize the part so it doesn’t spawn in the mountain. Although I did not make the code and it was in a free model I used in a game a long time ago.

1 Like

I am trying your code, I am attempting to fix it and only let them spawn in a certain radius. As this just spawns them randomly, and deletes them. The player is supposed to collect the gem. But thanks for the help!

1 Like

Don’t you want it to spawn randomly in a selected area?

1 Like


I want it to spawn under the yellow region, and not into the mountain.

1 Like
local map = game.Workspace.idk
local gem = workspace.idk

local parts = {}

for i,v in next,map:GetDescendants() do
	if (v:IsA("BasePart")) then
		table.insert(parts,v)
	end
end

local randompart = parts[math.random(1,parts)]
local cgem = gem:Clone()
cgem.Position = randompart.Positions + Vector3.new(0,25,0)
1 Like

oh, well the only way you’d get that to work with my script is by making a part that goes around the mountain and is unioned / negate / eroded in the middle so it technically doesn’t go through the mountain and the put the script in the part.

1 Like

Didn’t work.

local map = game.Workspace.Gems
local gem = game.ServerStorage.Gems['Stage-1']

local parts = {}

for i,v in next,map:GetDescendants() do
	if (v:IsA("BasePart")) then
		table.insert(parts,v)
	end
end

local randompart = parts[math.random(1,parts)]
local cgem = gem:Clone()
cgem.Position = randompart.Positions + Vector3.new(0,200.44,0)

---1822.345, 200.44, -3919.077
1 Like

Can I see the error :smile: :confused: ?

1 Like

what I mean:
00%20pm
pretend the blue part is the part with the gem spawn script in it and the yellow part is the mountain. That’s a solution to stop it spawning in the mountain.

1 Like

image

Well here’s the way I’d personally solve this problem:
First of all, select a random position using Random.new (I prefer this one) or math.random.
Secondly, do a ray cast from above the mountain to find the point on the mountain.
Thirdly, add a gem here and if you want, add a few studs to its Y position.

There are plenty of ways to go about this, for example you could pick predetermined positions on your own, or you could spawn your gems on flat areas.

1 Like

I’ll see if that works. Any examples you can show me?

Done now it works

local map = game.Workspace.Model
local gem = workspace.MeshPart

local parts = {}

for i,v in next,map:GetDescendants() do
	if (v:IsA("BasePart")) then
		table.insert(parts,v)
	end
end

local randompart = parts[math.random(1,#parts)]
local cgem = gem:Clone()
cgem.Parent = workspace
cgem.Position = randompart.Position + Vector3.new(0,200,0)

Problem was that i typed

local randompart = parts[math.random(1,parts)]
--Instead 
local randompart = parts[math.random(1,#parts)]
1 Like

This might help: Region3

EDIT:

Sorry I can’t help explain it as I’m on mobile.

3 Likes

Ohh yeah, this is also good Region3.

1 Like

Well the only large rule of this category are to not spoon feed code (and tbh I’m too lazy to write something for you)… Read documentation and figure it out yourself. If you get stuck, ask for help and I’ll be happy to help! :smile:

1 Like

It spawned REALLY far from the mountain. For the random, could we do something around this position
-1822.345, 200.44, -3919.077
where it would spawn there or around it