So I have several spawn points, when activated my code will select a random spawn point, inside each spawn point is a BoolValue “InUse” when true the spawn is occupied when false the spawn is available to use. The code check if the value is false and if so it will spawn an npc at that location.
The problem is if the InUse value is true then I need to randomly select another spawn until I find one that is not in use.
-- Detecting when a player enters the vehicle
if p == "Occupant" then
if script.Parent.VehicleSeat.Occupant ~= nil then
local player = game.Players:FindFirstChild(script.Parent.VehicleSeat.Occupant.Parent.Name)
local Spawns = Locations:GetChildren()
local Pick = Spawns[math.random(1,#Spawns)] -- Picking a random spawn point
if Pick.InUse.Value == false then
local Subject = Subject:Clone()
Subject.Parent = Group
Subject.Owner.Value = player.Name wait()
Subject.HumanoidRootPart.Position = Pick.Position
Subject.Detector.Position = Subject.HumanoidRootPart.Position
end
end
end
That is a good idea, when the spawn is picked it should be removed from the available spawns list. I may go with this instead but I will give others time to reply.
local Locations = workspace.CriminalSpawns
local Subject = game.ServerStorage.Criminal-- Detecting when a player enters the vehicle
if p == "Occupant" then
if script.Parent.VehicleSeat.Occupant ~= nil then
local player = game.Players:FindFirstChild(script.Parent.VehicleSeat.Occupant.Parent.Name)
if player then
local Spawns = {}
for _, instance in pairs(Locations:GetChildren()) do
if instance.InUse.Value == false then
table.insert(Spawns, instance)
end
end
if #Spawns > 0 then
local Pick = Spawns[math.random(1,#Spawns)] -- Picking a random spawn point
local Subject = Subject:Clone()
Subject.Parent = Group
Subject.Owner.Value = player.Name wait()
Subject.HumanoidRootPart.Position = Pick.Position
Subject.Detector.Position = Subject.HumanoidRootPart.Position
table.remove(Spawns, table.find(Pick))
end
end
end
end