Try this:
local timePeriod = 15
local despawnTime = 10
debris = game:GetService("Debris")
while true do
task.wait(timePeriod);
local spawns = script.Parent:GetChildren();
while true do
spawnPoint = spawns[math.random(#spawns)];
if spawnPoint:IsA("BasePart") and spawnPoint:FindFirstChild("occupied") and spawnPoint.occupied.Value == false then
spawnPoint.occupied.Value = true
break
end
task.wait() -- just in case all spots are occupied it won't timeout
end
local object = game.ServerStorage.SpawnItems.apol:Clone()
object.Handle.Position = spawnPoint.Position
object.Parent = workspace
debris:AddItem(object, despawnTime)
end
Did some other misc changes such as:
-
wait(timePeriod)
→task.wait(timePeriod)
(new task library) -
spawns[math.random(#spawns)]
instead ofspawns[math.random(1, #spawns)]
- The if statement for determining if the spawn is occupied or not
- Parenting the object after setting it’s
Position
property (for performance reasons, see here) - Removed the
valid
variable and just broke the loop if the condition was met