diff --git a/src/lune/globals/typeof.rs b/src/lune/globals/typeof.rs index fb1099c..2d9b91a 100644 --- a/src/lune/globals/typeof.rs +++ b/src/lune/globals/typeof.rs @@ -2,14 +2,26 @@ use mlua::prelude::*; use crate::roblox::datatypes::extension::RobloxUserdataTypenameExt; +const REGISTRY_KEY: &str = "NetClient"; + pub fn create(lua: &Lua) -> LuaResult> { - lua.create_function(|lua, value: LuaValue| { - #[cfg(feature = "roblox")] - if let LuaValue::UserData(u) = &value { - if let Some(type_name) = u.roblox_type_name() { - return lua.create_string(type_name); + let original = lua.globals().get::<_, LuaFunction>("typeof")?; + #[cfg(feature = "roblox")] + { + lua.set_named_registry_value(REGISTRY_KEY, original) + .expect("Failed to store typeof function in registry"); + lua.create_function(|lua, value: LuaValue| { + if let LuaValue::UserData(u) = &value { + if let Some(type_name) = u.roblox_type_name() { + return lua.create_string(type_name); + } } - } - lua.globals().get::<_, LuaFunction>("typeof")?.call(value) - }) + let original_fn: LuaFunction = lua + .named_registry_value(REGISTRY_KEY) + .expect("Missing typeof function in registry"); + original_fn.call(value) + }) + } + #[cfg(not(feature = "roblox"))] + original }