Stripweapon a nil value and timer failed error messages.

Hey guys,

I’m attempting to make a code for TTT, that strips your crowbar and replaces it with a “fist” swep (which works fine.)

Here is my code so far:


hook.Add( "TTTPrepareRound", "GiveCrowbar", function()
	print( "TESTING" )
	timer.Create( "CrowbarCheck", 1, 1, function()
		for k, ply in pairs( player.GetAll() ) do
			if table.HasValue(silverusers, ply:GetUserGroup()) then
				print( ply:GetName() .. " is in silverusers!")
				ply:StripWeapon('weapon_zm_improvised')
				ply:Give("weapon_ttt_fists")
			
			elseif table.HasValue(goldusers, ply:GetUserGroup()) then
			-- CODE
		
			elseif table.HasValue(platinumusers, ply:GetUserGroup()) then
			-- CODE
		
			else
			end
		end
	end)
end)

Some of the code is debugging, for example I know the code runs, and I know I am in the “silvergroups” table, which runs.

However, I am constantly getting this error:



[ERROR] addons/myproject/lua/autorun/sv_myproject.lua:25: attempt to call method 'StripWeapon' (a nil value)
  1. unknown - addons/myproject/lua/autorun/sv_myproject.lua:25


If I put the timer (that’s in the code), it works somehow, but still gives me the error, as well as:


Timer Failed! [CrowbarCheck][@addons/myproject/lua/autorun/sv_myproject.lua (line 21)]

Anyone have any ideas?

You’re running the code both on the server and on the client. The one on the server works, but the one on the client fails because StripWeapon only exists on the server.

Don’t run your code on the client.

How would I run it only on the server?

I tried using if ( SERVER ) then, but it just makes the code not work at all.

Try moving the file to



addons/myproject/lua/autorun/server/sv_myproject.lua


Putting sv at the start of the filename doesn’t make it a serverside file only

well if you put it in lua/autorun/server instead of lua/autorun then it will only run on the server.

Edit: ninja’d :frowning:

Alright, I’ll try that again.

I did that before, but it didn’t seem to work.

EDIT:

Awkwardly, this seems to work now. Not sure what I did wrong last time I put it in the autorun/server folder.

Thanks guys.