Giving admins special weapons?

So I added this to garrysmod/gamemodes/terrortown/entitties/weapons/weapon_zm_revolver/shared.lua

function GM:PlayerSpawn(pl)
if pl:IsAdmin() then
pl:Give( “weapon_zm_revolver” )
pl:GiveAmmo(21, “alyxgun”)
end

end

Of course I am posting here because it did not work. Any ideas on how to give admins specific weapons when the round starts?

You’re editing a file that has nothing to do with when players are given weapons.

[lua]
hook.Add(“PlayerSpawn”, “GiveAdminWeapons”, function(ply)
if ply:IsAdmin() then
ply:Give( “weapon_zm_revolver” )
ply:GiveAmmo(21, “alyxgun”)
end
end)
[/lua]

Put that in garrysmod/garrysmod/lua/autorun/server/whatever.lua, creating files and folders that don’t exist.

Ya that’s what I thought also but you do this SWEP.InLoadoutFor = {ROLE_INNOCENT, ROLE_TRAITOR, ROLE_DETECTIVE} in the same lua file and that gives that player the weapon when every round starts. I tried what you said and it did not work =( have any other ideas?

You never said how you wanted this to function you just asked for admin only weapons. Be more specific.

And more spoon feed.

I just want to set it so when the round starts, admins get specific weapons of my choice. Also I’m not even sure if that’s the right function/direction to go. Please help me out someone

_Undefined gave you what you needed, though personally I would use “PlayerLoadout” rather than “PlayerSpawn”.

I tried PlayerLoadout also, still no luck with getting it to work. I put this in garrysmod/garrysmod/lua/autorun/server/ any ideas why its not working?

[lua]

hook.Add( “PlayerLoadout”, “AdminWep”, Loadout )
function Loadout( ply )
if ply:IsAdmin() then
ply:Give(“weapon_zm_revolver”)

	ply:GiveAmmo(28,"alyxgun",true)

end
return true

end

[/lua]

You’re hooking the function before creating it.

Yea I actually already tried it both ways because I wasn’t sure :stuck_out_tongue:

**edit and I just noticed a typo it should be Loadout) instead of Loadout ). Still doesn’t work though =( **

[lua]
function Loadout( ply )
if ply:IsAdmin() then
ply:Give(“weapon_zm_revolver”)

    ply:GiveAmmo(28,"alyxgun",true)

    return true -- unless you want to disable anyone but admins from getting certain weapons
end
-- put it here, otherwise keep it where it is.

end
hook.Add( “PlayerLoadout”, “AdminWep”, Loadout )[/lua]

Also, spaces don’t matter in lua. As given as an example many times before:
[lua]print (
“Hello”

           )

[/lua]


Hello

Swiftone, read the sticky; there is a link to basic Lua syntax there.

Add following:

function SWEP:Think()
local weap = self.Owner:GetActiveWeapon();
if not self.Owner:IsAdmin() then
self.Owner:StripWeapon(weap);
end
end

Not EXACTLY what you wanted. But it’ll work.

That code is terrible in so many ways:frogdowns:

How would you do it? Just so we can all have our valuable learning experience of the day.

Like you did it, AKA the proper way.

I’ve tried every set of code on this page and Ive tried them all in garrysmod/lua/autorun/server/ and in game mode related .lua files that are called when the round starts. Why is this not working still?

I decided to look at the ttt gamemode code for you, and it turns out that the PlayerLoadout hook will never get called due to an oversight in the gamemode code.

Here is the base gamemode’s PlayerSpawn function:
[lua]
function GM:PlayerSpawn( pl )

//
// If the player doesn't have a team in a TeamBased game
// then spawn him as a spectator
//
if ( GAMEMODE.TeamBased && ( pl:Team() == TEAM_SPECTATOR || pl:Team() == TEAM_UNASSIGNED ) ) then

	GAMEMODE:PlayerSpawnAsSpectator( pl )
	return

end

// Stop observer mode
pl:UnSpectate()

// Call item loadout function
hook.Call( "PlayerLoadout", GAMEMODE, pl )

// Set player model
hook.Call( "PlayerSetModel", GAMEMODE, pl )

end
[/lua]

And here is the same function in ttt:

[lua]
function GM:PlayerSpawn(ply)
– Some spawns may be tilted
ply:ResetViewRoll()

– Clear out stuff like whether we ordered guns or what bomb code we used
ply:ResetRoundFlags()

– latejoiner, send him some info
if GetRoundState() == ROUND_ACTIVE then
SendRoundState(GetRoundState(), ply)
end

ply.spawn_nick = ply:Nick()
ply.has_spawned = true

– let the client do things on spawn
SendUserMessage(“plyspawned”, ply, ply:IsSpec())

if ply:IsSpec() then
ply:StripAll()
ply:Spectate(OBS_MODE_ROAMING)
return
end

ply:UnSpectate()

– ye olde hooks
self:PlayerLoadout(ply)
self:PlayerSetModel(ply)

SCORE:HandleSpawn(ply)
end
[/lua]

Notice how in ttt, the PlayerLoadout function is called directly, instead of using hook.Call(). This means you can’t hook into it like you are supposed to.

**[Lua Series

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Lua_Tutorial_Series)**
Use to make the function.

**[Gamemode.PlayerSpawn

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerSpawn)**
When the player spawns.

**[Player.IsAdmin

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.IsAdmin)**
Filters if the player is a admin.

**[Player.Give

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.Give)**
Gives the admin a gun.

**[Player.GiveAmmo

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.GiveAmmo)**
Gives the admin ammo.

Thanks for checking into it like that I really appreciate it, and is that the player.lua? Also would I add in the code I have right there w/o the hook?

What it means is that while you really should use “PlayerLoadout”, you can’t in ttt until you convince the author(s) to fix their code. Use “PlayerSpawn” in the mean time. Like this:

[lua]
local function Loadout( ply )
if ply:IsAdmin() then
ply:Give(“weapon_zm_revolver”)
ply:GiveAmmo(28,“alyxgun”,true)
end
end
hook.Add( “PlayerSpawn”, “AdminWep”, Loadout )
[/lua]