How to make a tool spawn randomly with a set number of locations it can spawn at?

So I want to make a system for one of the items in the game I’m making to spawn randomly at one of 5 places around the map. Problem is, I don’t know how to script it to make it randomly spawn… I can make a script to clone the tool, yeah, but getting it to randomly be at one of 5 locations is not a thing I can think of how to do.

Important Info for this: This is the map of the game area, with the 5 neon pillars marking where the item’s spawn locations are.


Positions of each of them:
Spot 1: 363.092, 45.125, -287.518
Spot 2: -269.077, 35.778, 234.314
Spot 3: 349.157, 40.591, 232.632
Spot 4: 21.266, 61.391, -451.484
Spot 5: 25.69, 34.551, 62.07

2 Likes

A way you could do it is to put the pillars in a folder, and in your script,

  • Make a table variable
  • Loop through the folder with all the parts
  • Store their CFrames into the table variable
  • Clone the tool, get a random position from that table and set it to the tool’s handle CFrame and parent it to workspace

…I don’t understand. Could that be dumbed down for me so I can understand it better?

Basically to explain it in shorter terms

You need a table variable, which is something like this local tbl = {}, this will used to store all the CFrames of the pillars. Now, we’ll assume you have a folder in workspace called Pillars, that store all of the pillars to spawn a tool on, to be more specific, the top part of them. To get their CFrames and store them in the table, you’d just do

for _,part in pairs(folder:GetChildren()) do
	table.insert(tbl, part.CFrame)
end

Now after this, you’d just clone the tool, set its cframe via this

tool.Handle.CFrame = tbl[math.random(#tbl)] * CFrame.new(0,1,0)

Which gets a CFrame from the table and adds 1 stud in the y axis so it wont spawn inside of the pillar and then just parent it to workspace

The code at the end would look like this

local tbl = {}
local folder = workspace.Pillars
local tool = --Your tool's location

for _,part in pairs(folder:GetChildren()) do
	table.insert(tbl, part.CFrame)
end

local clone = tool:Clone()
tool.Handle.CFrame = tbl[math.random(#tbl)] * CFrame.new(0,1,0)
tool.Parent = workspace
4 Likes

Ah okay, will test it out. Will get back and reply once it’s tested.

1 Like

So uh, where do I put the script into? Just the workspace?

You can either put it in ServerScriptService or workspace, whichever one works for you

Putting it in workspace worked, thank you.

I would recommend marking my post as the solution since it has helped out in figuring out your issue! If you have anymore issues don’t be afraid to make another post!

I forgot about marking it as the solution, oops.

1 Like

Haha, it’s fine! Least you eventually realised! Without a solution, people would’ve thought your post still needs help when it doesn’t anymore and they would’ve also reminded you as well!

1 Like

how could you add a delay to this or detect if any player in the game picks it up, then it will not clone itself?

I am also struggling on this too