Need help, making players spawn with weapons

Hi, so I was coding on my Trouble in Terrorist Town server and trying to get certain players spawn with weapons based off their steamids, I was having trouble, please note that I know that I need to put the steamids in, I simply removed them before posting this. This was the code I was using:



local function GiveWeapon(ply)
if ply:SteamID() == "Steamid here" then
        ply:Give("weapon_ttt_famas")
	ply:Give("weapon_zm_revolver")
	ply:Give("weapon_tttbasegrenade")
	return true

elseif ply:SteamID() == "Steamid here" then
	ply:Give("weapon_zm_rifle")
	ply:Give("weapon_zm_revolver")
	ply:Give("weapon_tttbasegrenade")
	return true
	end
end


Remember to always use code tags around your code on the forums.

You want to put this in a playerspawn/playerloadout hook to start off with, then you could have a table containing all of the SteamID of players you want to spawn with those weapons on your server, and use table.HasValue() to check this.


rank1 = {"STEAMID","STEAMID","etc"}
rank2 = {"STEAMID","STEAMID","etc"}

--Each table for ranks would look something like the above.

if table.HasValue(rank1, ply:SteamID()) then
    ply:Give("weapon_ttt_famas")
    ply:Give("weapon_zm_revolver")
    ply:Give("weapon_tttbasegrenade")
    return true
elseif table.HasValue(rank2, ply:SteamID()) then
    ply:Give("weapon_zm_rifle")
    ply:Give("weapon_zm_revolver")
    ply:Give("weapon_tttbasegrenade")
    return true
end

Somebody correct me if I posted anything wrong, there probably is a better way to do this so wait and see if there are other replies. Remember the above code is just how it works not the final code, you need to set that up.

Looks fine, but it really should be put in the PlayerSpawn hook. Also, it’s really pointless to use elseif; what’s the point of having two tables if you’re going to put an identical SteamID in both?

The point was the tables would contain different steam ids. I just didn’t want to go on the internet and look up random peoples SteamID’s to put in an example table. I assumed the OP wanted multiple groups since he had two different sets of weapons he wanted to give out.

I’d recommend doing something like this if you’re going to be using groups:

http://forum.facepunch.com/showthread.php?t=1375045&p=44219928&viewfull=1#post44219928

Essentially direct access into a table instead of searching.

I don’t know how to make a hook, I’m really only starting coding. Also, does it need to be anywhere specific in the files, or can it be placed anywhere?

Creating a hook is as easy as creating a
[lua]function GM:MyNewHook( any_args, go_here )
// … code that does stuff
end[/lua]

Then you call [lua]hook.Call( “MyNewHook”, GAMEMODE, any_args, go_here );[/lua] where you want it to execute.

That part is done if you’re writing an addon or game-mode and want others to be able to use your hooks…

For the purpose of this thread, you’d add a hook into an already existing hook: PlayerLoadout or PlayerSpawn like so:

[lua]hook.Add( “PlayerSpawn”, “MyUniqueNameForPlayerSpawn”, function( Player )
// your code here.
end );[/lua]

Also, make sure if you’re making a hook using function GM:MyNewHook like Acecool said, it’s completely new and you aren’t overriding a default function.

Also, like Acecool said, you could put the function inside the hook, or, you could make the function, then hook.Add afterwards. Example:


function giveWeapon( ply )
   --code
end

hook.Add( "PlayerSpawn", "Unique_Name", giveWeapon )

Both ways work, and it’s really a personal preference to what you’d like to use.

So, I have this in a random file, I’m not sure if that will affect it but it’s still not working…




hook.Add("PlayerSpawn", "Unique_Name", GiveWeapon )

local function GiveWeapon(ply)

rank1 = {"STEAM_0:0:00000000"}
rank2 = {"STEAM_0:0:00000000"}

if table.HasValue(rank1, ply:SteamID()) then
        ply:Give("weapon_ttt_famas")
	ply:Give("weapon_zm_revolver")
	ply:Give("weapon_ttt_confgrenade")
	return true

elseif table.HasValue(rank2, ply:SteamID()) then
	ply:Give("weapon_zm_rifle")
	ply:Give("weapon_zm_revolver")
	ply:Give("weapon_tttbasegrenade")
	return true
	end


The reason that’s not working is because you’re adding the hook before the function exists.
Add the hook after you declare the function.

[editline]16th March 2014[/editline]






local function GiveWeapon(ply)

        rank1 = {"STEAM_0:0:00000000"}
        rank2 = {"STEAM_0:0:00000000"}

        if table.HasValue(rank1, ply:SteamID()) then
                ply:Give("weapon_ttt_famas")
        	ply:Give("weapon_zm_revolver")
        	ply:Give("weapon_ttt_confgrenade")
        	return true
        elseif table.HasValue(rank2, ply:SteamID()) then
        	ply:Give("weapon_zm_rifle")
        	ply:Give("weapon_zm_revolver")
        	ply:Give("weapon_tttbasegrenade")
        	return true
        end
end
hook.Add("PlayerSpawn", "Unique_Name", GiveWeapon )


You also forgot one end.

Here’s a way to put that into a direct-access method which is many times quicker than using a O( n ) based search.
[lua]local RankedPlayersBySteamID = { };

// Easier way to add - can direct access via this method. // Just add the rank as the value.
RankedPlayersBySteamID[“STEAM_0:0:00000000”] = 1;
RankedPlayersBySteamID[“STEAM_0:0:00000001”] = 2;
RankedPlayersBySteamID[“STEAM_0:0:00000002”] = 2;
RankedPlayersBySteamID[“STEAM_0:0:00000003”] = 2;
RankedPlayersBySteamID[“STEAM_0:0:00000004”] = 1;
RankedPlayersBySteamID[“STEAM_0:0:00000005”] = 0;

hook.Add(“PlayerSpawn”, “Unique_Name”, function( ply )
local _rank = RankedPlayersBySteamID[ ply:SteamID( ) ] || 0;

    if ( _rank == 0 ) then
            // return false; -- Don't return, you could show a message saying they're not special, or give them something random like a random grenade...
    elseif ( _rank == 1 ) then
            ply:Give("weapon_ttt_famas")
    	ply:Give("weapon_zm_revolver")
    	ply:Give("weapon_ttt_confgrenade")

    	return true;
    elseif ( _rank == 2 ) then
    	ply:Give("weapon_zm_rifle")
    	ply:Give("weapon_zm_revolver")
    	ply:Give("weapon_tttbasegrenade")

    	return true;
    end

end );[/lua]

You could extend that further and link the number rank to another table which holds tables of weapons they spawn with and then just do a for loop to give weapon…

[lua]local WeaponsTheyGet = { };
WeaponsTheyGet[ 1 ] = { “weapon_ttt_famas”, “weapon_zm_revolver”, “weapon_ttt_confgrenade” };
WeaponsTheyGet[ 2 ] = { “weapon_zm_rifle”, “weapon_zm_revolver”, “weapon_tttbasegrenade” };

if ( WeaponsTheyGet[ _rank ] ) then
for k, v in pairs( WeaponsTheyGet[ _rank ] ) do
ply:Give( v );
end
end
[/lua]

Although, this type of stuff ( giving weapons and armor, etc ) should be done in PlayerLoadout. PlayerSpawn and PlayerLoadout are typically called together unless the original function was overwritten without a hook.Call being used…