From 6895b66550f55e658e680bb5afd3977fa4b80b0b Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sat, 25 Mar 2023 16:46:46 +0100 Subject: [PATCH] Fix unnecessary error message printing for destroyed instances --- CHANGELOG.md | 7 ++++ packages/lib-roblox/src/instance/mod.rs | 12 ++++--- .../instance/methods/ClearAllChildren.luau | 33 +++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 tests/roblox/instance/methods/ClearAllChildren.luau diff --git a/CHANGELOG.md b/CHANGELOG.md index 778c546..56e9a12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Fixed + +- Fixed accessing a destroyed instance printing an error message even if placed inside a pcall +- Fixed cloned instances not having correct instance reference properties set (`ObjectValue.Value`, `Motor6D.Part0`, ...) + ## `0.6.2` - March 25th, 2023 This release adds some new features and fixes for the `roblox` built-in. diff --git a/packages/lib-roblox/src/instance/mod.rs b/packages/lib-roblox/src/instance/mod.rs index a1580a9..7f2e716 100644 --- a/packages/lib-roblox/src/instance/mod.rs +++ b/packages/lib-roblox/src/instance/mod.rs @@ -250,10 +250,9 @@ impl Instance { fn ensure_not_destroyed(&self) -> LuaResult<()> { if self.is_destroyed() { - Err(LuaError::RuntimeError(format!( - "Tried to access destroyed instance '{}'", - self - ))) + Err(LuaError::RuntimeError( + "Instance has been destroyed".to_string(), + )) } else { Ok(()) } @@ -704,7 +703,10 @@ impl Instance { impl LuaUserData for Instance { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { - methods.add_meta_method(LuaMetaMethod::ToString, userdata_impl_to_string); + methods.add_meta_method(LuaMetaMethod::ToString, |lua, this, ()| { + this.ensure_not_destroyed()?; + userdata_impl_to_string(lua, this, ()) + }); methods.add_meta_method(LuaMetaMethod::Eq, userdata_impl_eq); /* Getting a value does the following: diff --git a/tests/roblox/instance/methods/ClearAllChildren.luau b/tests/roblox/instance/methods/ClearAllChildren.luau new file mode 100644 index 0000000..dad9ce9 --- /dev/null +++ b/tests/roblox/instance/methods/ClearAllChildren.luau @@ -0,0 +1,33 @@ +local roblox = require("@lune/roblox") :: any +local Instance = roblox.Instance + +local root = Instance.new("Model") +local child1 = Instance.new("Part") +local child2 = Instance.new("Part") + +child1.Parent = root +child2.Parent = root + +assert(#root:GetChildren() == 2) +assert(root:GetChildren()[1] == child1) +assert(root:GetChildren()[2] == child2) + +root:ClearAllChildren() + +assert(#root:GetChildren() == 0) + +assert(not pcall(function() + return child1.Name +end)) + +assert(not pcall(function() + return child1.Parent +end)) + +assert(not pcall(function() + return child2.Name +end)) + +assert(not pcall(function() + return child2.Parent +end))