Freezing all Props

Hey there again!
Ive searched for some spare lines of code for a funtion that includes freezeing all the props, but it does not work.


        local Ent = ents.FindByClass("prop_physics")
                for _,Ent in pairs(Ent) do
                        if Ent:IsValid() then
                                local phys = Ent:GetPhysicsObject()
                                phys:EnableMotion(false)
                        end
                end


Thanks already.

Your VALUE in “for key, value” can’t be named the same as your table containing entities.

Change Ent in:


for _,Ent do

To just “E”

So to that?



 for _,E in pairs(Ent) do


Edit: That didnt work out at all :confused:

Well, you changed this to match your modification, right?



if Ent:IsValid() then
     local phys = Ent:GetPhysicsObject()
     phys:EnableMotion(false)
end


Oh, hang on.
Ill try that

Unless you are using the Ent table anywhere else, just use ents.FindByClass(“prop_physics”) inside the loop




for _,E in pairs( ents.FindByClass("prop_physics") ) do



I get this error:




[ERROR] addons/cleaner/lua/autorun/autoclean_server.lua:23: Tried to use a NULL physics object!
  1. EnableMotion - [C]:-1
   2. unknown - addons/cleaner/lua/autorun/autoclean_server.lua:23



on the line with
phys:EnableMotion(false)

Heh, Help?

make sure you check if it exists first

Heres the most important things of the script, including the props.



function ServerStartup()
	local delay = 10

	timer.Create( "FreezingProgress", delay, 0, function()	
        for _,E in pairs( ents.FindByClass("prop_physics") ) do
		if E:IsValid() then
			local phys = E:GetPhysicsObject()
			phys:EnableMotion(false)
		end
        end
	end)
end
hook.Add( "Initialize", "Cleaning", ServerStartup)

What is not correct with it?

You need to check if the physics object is valid. So



local phys = E:GetPhysicsObject()
if phys:IsValid() then
	phys:EnableMotion(false)
end


Like that?



	timer.Create( "FreezingProgress", delay - delay/2, 0, function()	
    for _,E in pairs( ents.FindByClass("prop_physics") ) do
		if E:IsValid() then
			local phys = E:GetPhysicsObject()
			if phys:IsValid() then
				phys:EnableMotion(false)
			end
		end
    end
	end)


[editline]20th July 2015[/editline]

This thing is now not doing anything.

you don’t need that many IsValid checks, if you loop all entities it obviously exists

just try:



  if phys:IsValid() then
     phys:EnableMotion(false)
     phys:SetVelocity(Vector(0, 0, 0)) 
     phys:SetMoveType(MOVETYPE_NONE)
  end


if that doesn’t work, debug your code - add prints after each if statement and see if it’s passed. make sure your timer is running too.