InitPostEntity to freeze weapons spawned by map?

Hey, I need some help figuring out how to freeze weapon entities with either ent:GetPhysicsObject():EnableMotion(false) or ent:GetPhysicsObject():Sleep(), so far I’ve come with this

[lua]
if SERVER then
hook.Add(“InitPostEntity”,“FreezeProps”,function()
for k,v in pairs(ents.GetAll()) do
if (!v:IsWeapon()) then continue; end
v:GetPhysicsObject():EnableMotion(false);
end
end
end[/lua]

but it gives me an error saying it is expecting a ) at the end of the second line, which when added gives me an error about an extra ) at the end. I’m naming it freeze_props.lua and putting it in lua/autorun/server.

What am I doing wrong? Keep in mind that I have little lua knowledge so please go easy on me :3

EDIT: btw thanks to G4MB!T for part of the code.

You never close the parenthesis opened by hook.Add

Well I added a ) after (), but then it gives me an error about an extra ) :frowning:

No, add it to the second to last end.

[lua]
if SERVER then
hook.Add(“InitPostEntity”,“FreezeProps”,function()
for k,v in pairs(ents.GetAll()) do
if (!v:IsWeapon()) then continue; end
v:GetPhysicsObject():EnableMotion(false);
end
end)
end[/lua]

Like that?

EDIT: offtopic but this dude who rated my post dumb, go read the messages on his profile

Yes

Alright thanks, it fixed the errors but the weapons are not freezing on spawn, what gives?

EnableMotion never worked with weapons as I remember

[editline]28th March 2015[/editline]

Seriously, it doesn’t work properly

Are you using the same code they are talking about? Then you are using it wrong… This code is meant for items that are spawned in map, when it loads. For what you are doing now, you would have to use OnEntityCreated hook.

I guess it checks out (show us your code).

Maybe there’s an addon or something, what are other alternatives? What about OnEntityCreate, maybe the gamemode itself creates it and it could work?

All weapons are unfrozen after a certain delay after you spawn the weapon ( Why is beyond me ).

I have already released numerous times my fix I made for my SWEP base:



function SWEP:AcceptInput( name, activator, caller, data )
	if ( name == "ConstraintBroken" && self:HasSpawnFlags( 1 ) ) then
		local phys = self:GetPhysicsObject()
		if ( IsValid( phys ) ) then phys:EnableMotion( false ) end
	
		local newflags = bit.band( self:GetSpawnFlags(), bit.bnot( 1 ) )
		self:SetKeyValue( "spawnflags", newflags )
	end
end

Mmmh, I tried that but it gives me an error about indexing global SWEP <a nil value>. I saved that piece of code to freeze_props.lua in autorun/server.

It’s not meant to be used like that, that is added to the SWEP’s file itself.

Entity:PhysicsDestroy

You are fucking awesome, it absolutely works!!!

Yep, that did it. Thank you a lot boys, you can close this or whatever you do with solved threads :smiley: