Randomizing tasks for an among us type game

  1. What do you want to achieve?
    I’m trying to make a script that copies a random task part and positions it to a random spot, tagged using a placeholder part.

  2. What is the issue?
    Everything goes well, except that sometimes the parts would pick the same spot and overlap each other.

  3. What solutions have you tried so far?
    I’ve tried other stuff like deleting the placeholder once it has been occupied but it still wouldn’t work.

Here is the script:

So, you don’t want them to overlap basically?

Code
function taskcontroller.placeTasks()
   local getPlacements = workspace.TaskPlacements:GetChildren()
   local getTasks = rs.tasks:GetChildren()
   local positionstaken = {}

   for i = 1, #getTasks do
      local chosenTask = getTasks[math.random(1,#getTasks)
      local randomPlacement

      local function set()
         local num = math.random(1,#getPlacements)

         if not positionstaken[getPlacements[num]] then
             randomPlacement = getPlacements[num]
             table.insert(positionstaken, getPlacements[num])
         else
             set()
             -- Be careful, this can crash your game if all placements are taken!
         end
      end
      set()

      local copy = chosenTask:Clone()
      copy.Position = randomPlacement.Position
      copy.Parent = workspace.CurrentTasks
   end
end

Edit : When you said “i tried deleting them when they are occupied”, I just realized theres a way more better way than this.

Delete randomPlacement and move local getPlacements inside the for loop

2 Likes

So something like this?

Edit: Nevermind I think I fixed it! Thanks for the help!

1 Like