This is a small module I created for a few of my games, and thought I would release it here for other developers to use/reference.
About:
This Spawn-System allows you to customize where a Roblox Character will respawn without having to fight the default Roblox Spawn-System.
Toolbox Link:
Usage:
Simply place it inside Server Script Service to run. Require it via:
local SpawnSystem = require(game.ServerScriptService:FindFirstChild("SpawnSystem", true))
By default the Spawn-System attempts to emulate Roblox’s native default spawn logic via:
- Check if Player has a RespawnLocation set
- Get List of all valid Team spawns and Neutral spawns
- Pick spawn randomly from list
Examples:
The logic required to pick a spawn location can be easily overwritten:
-- Custom spawn location
SpawnSystem.ChooseSpawn = function(player: Player) : Vector3|CFrame|SpawnLocation?
return Vector3.new(0, 100, 0)
end
Additionally, if you wish to run custom logic upon character spawning, this can be done easily as well:
-- Custom teleportation callback (eg. If Humanoid Rootpart is not used)
SpawnSystem.RequestTeleport = function(player: Player, transform: CFrame)
CustomTeleportRemote:FireClient(player, transform)
end
You can easily query all spawn locations (neutral and team spawns) in the game:
-- Print spawn locations
print(SpawnSystem.SpawnLocations)
print(SpawnSystem.NeutralSpawns)
print(SpawnSystem.TeamSpawns)
For convenience you can also force respawn players:
-- Respawn all players in the game
SpawnSystem:RespawnPlayers(game.Players:GetPlayers())
As an additional example, I am using this module in one of my current projects to “pick a spawn that is farthest away from all players”, great for Free-For-All gamemodes:
local function getFreshSpawnLocation(IgnorePlayers)
local SpawnsFarEnoughAway = {}
local FarthestSpawn = nil
local FarthestDistance = 0
for _,v in pairs(Spawns) do
-- Get nearest player TO this spawn
local NearestPlayer = nil
local NearestPlayerDistance = math.huge
for _,p in pairs(game.Players:GetPlayers()) do
local CanProcess = true
for _,i in pairs(IgnorePlayers) do
if ( i == p ) then
CanProcess = false
break
end
end
if ( not CanProcess ) then
continue
end
local Position = p.Character and p.Character.PrimaryPart and p.Character.PrimaryPart.Position or Vector3.new()
local DistanceToSpawn = (Position - v.Position).Magnitude
if ( DistanceToSpawn < NearestPlayerDistance ) then
NearestPlayerDistance = DistanceToSpawn
NearestPlayer = p
end
end
-- Get spawn that is far away
if ( NearestPlayerDistance > 800 ) then
SpawnsFarEnoughAway[#SpawnsFarEnoughAway+1] = v
end
-- Get spawn with FARTHEST nearest player
if ( NearestPlayerDistance > FarthestDistance ) then
FarthestDistance = NearestPlayerDistance
FarthestSpawn = v
end
end
-- If we have spawns that are "far enough" away, pick one
if ( #SpawnsFarEnoughAway > 0 ) then
return SpawnsFarEnoughAway[math.random(#SpawnsFarEnoughAway)].Position
end
-- If we get here, no spawns are configured
if ( not FarthestSpawn ) then
error("Could not find spawn location for player")
end
-- Return the spawn that is farest away (no spawns with a player > 800 studs away OOF)
return FarthestSpawn.Position
end
SpawnSystem.ChooseSpawn = function(player: Player)
return getFreshSpawnLocation({player})
end