Trying to make my first lua code and I have some errors

Hi I am new to facepunch and new to coding my code is


local function chatCommand( ply, text, public )
	if (string.sub(text, 1, 9) == "/spectate") then
		 ply.StartedSpecPos = ply:GetPos()
		 ply.TheWeapons = ply:GetWeapons()
		 ply:StripWeapons()
		 ply:Spectate(OBS_MODE_ROAMING)
         return(false)
    end
    if (string.sub(text, 1, 11) == "/unspectate") then
         ply:UnSpectate()
		 ply:SetPos( ply.StartedSpecPos )
		 for k,v in pairs( ply.TheWeapons ) do v:Give() end
		return(false)
    end
end
hook.Add( "PlayerSay", "chatCommand", chatCommand );

It is really basic at the moment but I am getting the error
[ERROR] lua/first.lua:12: attempt to call method ‘Give’ (a nil value) 1. v - lua/first.lua:12 2. unknown - lua/includes/modules/hook.lua:84

If anyone knows how to solve this and actualy explain that would be nice thank you

for k,v in pairs( ply.TheWeapons ) do ply:Give( v:GetClass() ) end

[editline]7th June 2014[/editline]

What you were doing is you were calling :Give on a Weapon, and weapons don’t have such method.

To give a weapon to a player, you must do “Player:Give( weapon_class )”

ok i will be testing this out also this is for my client and if you can explain this to me it would be nice

Just updated my post, with fixed code too, sorry.

Ok thank you robotboy very much if this works i will be exteremely weapons im still a noob at coding

[editline]7th June 2014[/editline]

ok im now getting this error

[ERROR] lua/first.lua:12: Tried to use a NULL entity!

  1. GetClass - [C]:-1
  2. v - lua/first.lua:12
    3. unknown - lua/includes/modules/hook.lua:84

Here’s how to set up a spectator system: https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/spectating_players_system.lua.html

Make sure you kill the player and prevent them from respawning.

Chat command system - redirects ! and / to concommand: https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands.lua.html

Ok im sorry but im trying to do some basic things not copy and paste a code of off a website I can find myself, and this is just for on clientsite as i said this is my first lua script other then that hello world

That’s basically because you remove the weapons you just saved.



ply.TheWeapons = {}
for id, wep in pairs( ply:GetWeapons() ) do table.insert( ply.TheWeapons, wep:GetClass() ) end

and


for k,v in pairs( ply.TheWeapons ) do ply:Give( v ) end

That should do the trick.

thank you for that so basicly what it is doing is taking the id of the weapons and looping them in pairs then inserting a table of that?
works thank you so much