Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to randomly spawn a part, or a group of parts, around a map?

Asked by 2 years ago

I've been trying to figure out how to spawn a group of parts, or one part, around a map randomly (an npc, a tree, a log something like that), and I can't really figure it out. I've tried looking it up in a few places, but nobody clarifies anything. If someone could help me, I'd be very grateful! Please also let me know where to put the script :) Thank you for your help in advance.

1 answer

Log in to vote
0
Answered by
bailley5 114
2 years ago
Edited 2 years ago

You can use math.Random to generate a random number between 1 and how many spawns in a folder (The folder will have to contain invisible parts that the items will randomly spawn at!)

local spawns = workspace.ItemSpawns:GetChildren()
local randomVal = math.random(1, #spawns)

This picks a random number inside a variable, which you can use for later on in the script

You can get the item and clone it, then parent it to the workspace:

local item = game.ReplicatedStorage.Item:Clone()
item.Parent = workspace

Finally you set the position of the cloned item to a random spawn then set the random spawn to a folder named "Taken" this is to make sure the spawn wont be used again (to prevent multiple items spawning in that location):

item.Position = spawns[randomVal].Position
spawns[randomVal].Parent = workspace.Taken

All together you should have:

local spawns = workspace.ItemSpawns:GetChildren()
local randomVal = math.random(1, #spawns)
local item = game.ReplicatedStorage.Item:Clone()
item.Parent = workspace
item.Position = spawns[randomVal].Position
spawns[randomVal].Parent = workspace.Taken

Have a great day, if this works make sure to mark it as the solution. If you have questions about the code, you can ask!

Ad

Answer this question