Changing collision rules within a callback is likely to cause crashes! Help with entities


AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

if (SERVER) then
    resource.AddFile("sound/aot/wire.mp3")
    resource.AddFile("sound/aot/wire_hit.mp3")
    resource.AddFile("sound/aot/wire_move.mp3")
    resource.AddFile("sound/aot/wire_shoot.wav")
end

local WireHit = Sound("aot/wire_hit.wav")


function ENT:Initialize()
    self:SetNWBool("Colide", false)
    self:SetNWBool("Sound", false)
    self:SetVar("weld", false)
    self:SetVar("moveweld", false)
    self.target = nil
    local bone = self.Owner:LookupBone("ValveBiped.Bip01_Head1")
    local pl = self:GetOwner()
    local aimvec = pl:GetAimVector()
    self.aimAng = self:GetAngles()
    self.Team = self.Owner:Team()
    self:SetModel("models/weapons/w_missile_launch.mdl")
    self:PhysicsInit(SOLID_VPHYSICS)
    self:SetAngles(self:GetAngles())
    self:SetPos(self:GetPos())
    self:SetCollisionGroup(COLLISION_GROUP_PLAYER)
    self:GetPhysicsObject():EnableGravity(true)
    self:SetSolid(SOLID_VPHYSICS)
    self:SetParent(self)
    self:SetModelScale(1, 0)
    self:SetColor(Color(0, 0, 0, 0))
    self:SetRenderMode(RENDERMODE_TRANSALPHA)
    local phys = self:GetPhysicsObject()
    phys:ApplyForceCenter(aimvec * 50000)
    local phys = self:GetPhysicsObject()

    if phys and phys:IsValid() then
        phys:EnableGravity(false)
        phys:SetMass(1)
        phys:EnableCollisions(true)
    end
end

function ENT:Think()
    local phys = self:GetPhysicsObject()

    if self:WaterLevel() ~= 0 then
        self:Remove()
    end

    if not self:GetVar("weld") then
        self:SetAngles(self.aimAng)
        phys:ApplyForceCenter(self.aimAng:Forward() * 50000)
    end

    if self:GetVar("moveweld") then
        if self.target:IsNPC() or self.target:IsPlayer() then
            if IsValid(self.target) then
                if self.target:Health() <= 0 then
                    self:Remove()
                end

                self:SetPos(self.target:GetPos() + self.distance)
                self:SetAngles(self:GetAngles())
            else
                self:Remove()
            end
        else
            if IsValid(self.target) then
                self:SetPos(self.target:GetPos() + self.distance)
                self:SetAngles(self:GetAngles())
            else
                self:Remove()
            end
        end
    end
end

function ENT:PhysicsCollide(data, phys)
    local hitEnt = data.HitEntity
    self:SetVar("weld", true)
    self:EmitSound(WireHit)

    if hitEnt:IsWorld() then
        self:SetVar("moveweld", false)
    else
        self:SetVar("moveweld", true)
        self.target = hitEnt
    end

    if not self:GetVar("moveweld") then
        self:SetPos(data.HitPos - data.HitNormal * -0.5)
        self:SetAngles(self:GetAngles())
        timer.Simple( 0, function() self:GetPhysicsObject():EnableMotion(true) end )
    else
        self:SetPos(data.HitPos - data.HitNormal * -1)
        self:SetAngles(self:GetAngles())
        timer.Simple( 0, function() self:GetPhysicsObject():EnableMotion(false) end ) -- timer.Simple( 0, function() self:GetPhysicsObject():EnableMotion(false) end )
        self.distance = self:GetPos() - self.target:GetPos()
    end

    self:SetNWBool("Colide", true)
    self:SetNWBool("Sound", true)
    timer.Simple( 0, function() self:SetCollisionGroup(COLLISION_GROUP_WORLD) end )


    function self:PhysicsCollide()
    end
end

function ENT:OnRemove()
    self:SetNWBool("Colide", false)
    self:SetNWBool("Sound", false)
    self:SetVar("weld", false)
    self:SetVar("moveweld", false)
    self.target = nil
end

Changing collision rules within a callback is likely to cause crashes!
This code is crashing my server, can someone can help me for that?

up…