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?
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
It’s not a map, it is a gem? I am a bit confused. And will the gems spawn in the mountain.?
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.
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!
Don’t you want it to spawn randomly in a selected area?
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)
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.
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
Can I see the error ?
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.
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)]
Ohh yeah, this is also good Region3.
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