Attempt to call method a nil value

I am trying my hand at lua, and I am making a simple swep which will freeze npcs and players. I don’t know how right the code is, and I keep getting problems with the NPC part in particular. It is line 43 and 57, and it says “attempt to call method ‘StopMoving’ a nil value”. I am not sure how to set it up right on that part. Thanks for any help! <3


SWEP.Author				= "-Matt-94"
SWEP.Contact				= ""
SWEP.Purpose				= "Freezes victims"
SWEP.Instructions			= "Left Click to Freeze, Right Click to Unfreeze"

SWEP.Spawnable				= true
SWEP.AdminSpawnable			= true

SWEP.ViewModel				= "models/weapons/v_pistol.mdl"
SWEP.WorldModel				= "models/weapons/w_pistol.mdl"

SWEP.Primary.ClipSize		        = -1
SWEP.Primary.DefaultClip	        = -1
SWEP.Primary.Automatic 		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.Clipsize 	        = -1
SWEP.Secondary.DefaultClip 	        = -1
SWEP.Secondary.Automatic 	        = false
SWEP.Secondary.Ammo 		= "none"

SWEP.NPC					= true
SWEP.PLY					= true

local ShootSound = Sound("Metal.SawbladeStick")

function SWEP:Reload()
end

function SWEP:Think()
end

function SWEP:PrimaryAttack()

	self.Weapon:EmitSound( ShootSound )

	local fz = self.Owner:GetEyeTrace()
	
	if fz.Entity:IsPlayer() then
		Player:Freeze(true)
		end
	if fz.Entity:IsNPC() then
		fz:StopMoving(true)
		end
end

function SWEP:SecondaryAttack()
	
	self.Weapon:EmitSound( ShootSound )

	local fz = self.Owner:GetEyeTrace()
		
	if fz.Entity:IsPlayer() then
		Player:Freeze(false)
		end
	if fz.Entity:IsNPC() then
		fz:StopMoving(false)
		end
end

Well, there’s a few problems here.

  1. There is no “StopMoving” function, so that’s why it errors. Plus, you were calling it on the trace, not the entity.

If you want to freeze an object, I would recommend doing:


entity:SetMoveType( MOVETYPE_NONE )

(http://wiki.garrysmod.com/?title=Entity.SetMoveType)

  1. You’re trying to call Freeze on “Player.” Player does not exist. The entity is fz.Entity, remember?

Ok thanks for replying!

  1. I was trying to use this: NPC:StopMoving( )

  2. Would I do fz.Freeze(true)?

An NPC is an entity, so it wouldn’t work regardless.

You would want to do fz.Entity:Freeze(true).

Ok so here is what I put:


SWEP.Author			= "-Matt-94"
SWEP.Contact				= ""
SWEP.Purpose				= "Freezes victims"
SWEP.Instructions			= "Left Click to Freeze, Right Click to Unfreeze"

SWEP.Spawnable				= true
SWEP.AdminSpawnable			= true

SWEP.ViewModel				= "models/weapons/v_pistol.mdl"
SWEP.WorldModel				= "models/weapons/w_pistol.mdl"

SWEP.Primary.ClipSize		        = -1
SWEP.Primary.DefaultClip	        = -1
SWEP.Primary.Automatic 		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.Clipsize 	        = -1
SWEP.Secondary.DefaultClip    	= -1
SWEP.Secondary.Automatic 	        = false
SWEP.Secondary.Ammo 		= "none"

SWEP.NPC					= true
SWEP.PLY					= true

local ShootSound = Sound("Metal.SawbladeStick")

function SWEP:Reload()
end

function SWEP:Think()
end

function SWEP:PrimaryAttack()

	self.Weapon:EmitSound( ShootSound )

	local fz = self.Owner:GetEyeTrace()
	
	if fz.Entity:IsPlayer() then
		fz.Entity:SetMoveType( MOVETYPE_NONE )
		end
	if fz.Entity:IsNPC() then
		fz.Entity:SetMoveType(MOVETYPE_NONE)
		end
end

function SWEP:SecondaryAttack()
	
	self.Weapon:EmitSound( ShootSound )

	local fz = self.Owner:GetEyeTrace()
		
	if fz.Entity:IsPlayer() then
		fz.Entity:SetMoveType( MOVETYPE_CUSTOM )		
		end
	
	if fz.Entity:IsNPC() then
		fz.Entity:SetMoveType(MOVETYPE_CUSTOM)
		end
end


It semi works but it only stops certain movements NPCs make. Like I can stop a headcrab from jumping, and it will look like it is walking in place. I can stop an antlion from flying but it can still attack and run. It is weird.

Also I was wondering if I did this right.

When I put “local fz = self.Owner:GetEyeTrace()” in there, does that make it so when I call fz it will use whatever I am looking at at the time? Like if I was pointing at a zombie, that would be what is in the variable?

I’m not entirely sure about entities, but for players I would use player.Freeze.

fz is just the player’s trace, so it’s whatever the player is looking at at the time. If you were looking at a zombie, then fz.Entity would be the zombie.

No matter what I am having problems with this :

weapons\freeze\shared.lua:58: attempt to call method ‘Freeze’ (a nil value)

the code is this:


if fz.Entity:IsNPC() then
		fz.Entity:Freeze(true)
		end

Am I just not doing syntax right? Or is this a completely wrong function to use? That Entity.SetMoveType doesn’t work very well so I need another solution.

Freeze() isn’t in the GMOD Lua API anymore. You’ll have to come up with another method. Perhaps set all their speeds to 0 until they you unfreeze them? That’s only for players, though.

Set their position to the initial position every tick.

You can use Player.Lock now.

Ok I did the get pos and set pos thing, it works, but only for like 1 tick or something. So would I use a “do” or “for” and do I need GM:Tick()? Also what are the arguments for GM:Tick()?

If you guys could post an example it would be much appreciated. I am just trying to get in the right frame of mind lol.

I hate to bump but I really need to figure this out.

Here is what I have been trying to do:



	if fz.Entity:IsNPC() then
		fz.Entity:GetPos()
		fz.Entity:SetPos(vector1) until i=1000
		return

It is supposed to keep looping, effectively keeping an NPC in its original position. Can you guys tell me what is wrong with it? Thanks!

and vector1 = fz.Entity:GetPos()

[lua]
function NPCMoveHook()
for k,v in pairs(ents.GetAll()) do
if v:IsNPC() then
if v.OldPos == nil then v.OldPos = v:GetPos() end
if v.OldPos ~= v:GetPos() then
v:SetPos(v.OldPos)
end
end
end
end
hook.Add(“Think”,“NPCMoveHook”,NPCMoveHook)
[/lua]

Trying to brainstorm other ways to stop the NPC moving. A think hook seems very unnecessary.

You could weld it to the world maybe? constraint.Weld(ent, GetWorldEntity(), 0, 0, 0, false)

Thanks for the suggestions guys, I haven’t quite nailed it down yet.

MegaJohnny can you give me some tips on how to use the constraint.Weld idea? Thanks man!

The function in my post is a link to the article, with a list of what the arguments are.

Use it pretty much exactly how I’ve used it in my post, replacing the first argument with a variable of the NPC you’re freezing. (ie fz.Entity)

As a bit more information on fz, it’s a table that has numerous bits of information about the line you traced. http://wiki.garrysmod.com/?title=Traceres

It’s not a variable of the entity you hit.

Sweet! The information on fz helps alot, I’m going to mess around with all that in a bit.