Skip to content

Instantly share code, notes, and snippets.

@OmegaExtern
Last active October 2, 2015 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OmegaExtern/e04e960e17e7d5c0c249 to your computer and use it in GitHub Desktop.
Save OmegaExtern/e04e960e17e7d5c0c249 to your computer and use it in GitHub Desktop.
Garry's Mod. Simple, yet safe spawn protection system. Scripted for beginner challenges (https://facepunch.com/showthread.php?t=1487912).
-- This code is for begginers; I have intentionally left out lots of things such as auto-refresh compatibility...
if ( !SERVER ) then
-- This script is mean to be executed/opened on the serverside!
ErrorNoHalt( "Not serverside" ) -- Tell an admin it is not run from environment where it is expected as.
return -- Exit/terminate a script.
end
-- ConVars to manage the spawn protection system:
local sv_spawnprotection = CreateConVar( "sv_spawnprotection", "1", { FCVAR_ARCHIVE, FCVAR_NOTIFY }, "Determines whether simple spawn protection is enabled or disabled" )
local sv_spawnprotection_delay = CreateConVar( "sv_spawnprotection_delay", "8", { FCVAR_ARCHIVE, FCVAR_NOTIFY }, "Determines how long should spawn protection last in seconds after a player has spawned" )
--[[-----------------------------------------------
GM:PlayerSpawn(Player)
AVAILABILITY: Server
DESCRIPTION: Called whenever a player spawned.
ARGUMENTS:
(1) Player, ply: The player who spawned.
-----------------------------------------------]]
hook.Add( "PlayerSpawn", "SimpleSpawnProtection.PlayerSpawn", function( ply )
if ( !sv_spawnprotection:GetBool() ) then
return -- Spawn protection is disabled.
end
-- Spawn protection is enabled.
ply:GodEnable() -- Enable god mode on the player.
timer.Simple( sv_spawnprotection_delay:GetInt(), function()
-- Timer will run this function only once (in background), automatically after sv_spawnprotection_delay:GetInt() seconds have passed (by default, after 8s).
if ( IsValid( ply ) ) then -- We must ensure a player object remained valid after delay (it would be invalid and cause script errors if a player have disconnected from the server)..
ply:GodDisable() -- Disable god mode on the player.
end
end )
end )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment