diff --git a/Entities/MovableObject.cpp b/Entities/MovableObject.cpp index eb1193d6c..b251cb0ac 100644 --- a/Entities/MovableObject.cpp +++ b/Entities/MovableObject.cpp @@ -533,16 +533,17 @@ int MovableObject::Save(Writer &writer) const ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void MovableObject::DestroyScriptState() { - if (ObjectScriptsInitialized()) { - RunScriptedFunctionInAppropriateScripts("Destroy"); - - if (m_ThreadedLuaState) { - std::lock_guard lock(m_ThreadedLuaState->GetMutex()); + if (m_ThreadedLuaState) { + std::lock_guard lock(m_ThreadedLuaState->GetMutex()); + + if (ObjectScriptsInitialized()) { + RunScriptedFunctionInAppropriateScripts("Destroy"); m_ThreadedLuaState->RunScriptString(m_ScriptObjectName + " = nil;"); - m_ThreadedLuaState->UnregisterMO(this); - m_ThreadedLuaState = nullptr; m_ScriptObjectName.clear(); } + + m_ThreadedLuaState->UnregisterMO(this); + m_ThreadedLuaState = nullptr; } }