Random gems spawn

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

It spawns really high up or left or right?

Forward from the mountain, so left.

Can you give me a small video clip of it?

Spawns there
Supposed to spawn there

Why not to insert a part at the centre of it and spawn the gem 10 by 10 area?

I will show you video example

local spawner = game.Workspace.Spawner
local gem = script.Spawner
local r = 10

while wait(1) do
	local gemclone = gem:Clone()
	local x,z = spawner.Position.X,spawner.Position.Z
	x,z = math.random(x - r,x + r),math.random(z - r,z + r)
	local pos = Vector3.new(x,spawner.Position.Y,z)
	gemclone.Parent = workspace
	gemclone.Position = pos
end


image

10 Likes

This worked. Thank you! Happy easter.

1 Like

You too mate cya next time also make it as solution so people won’t click this topic anymore.

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

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

FIxed your code a bit.

1 Like