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
what I mean:
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.
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.
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)]
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!
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
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
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