无效参数 #3(预期实例,得到字符串)

invalid argument #3 (Instance expected, got string)

我在搜索引擎中寻找这个问题的解决方案,但找不到。

虽然有一个几乎相同的 'Invalid Argument #2 (String Expected, got Instance)',但我尝试寻找反向修复它的方法,但它使用 tostring(),我的问题是关于最新功能:Instance Attributes,它不支持实例作为值,我唯一能做的就是将它转换为字符串,现在问题是关于 Part.Parent,它使用 Adornee/Instance或者我不知道解决方法的东西

local LocalPlayer   =   Players.LocalPlayer;
local Mouse         =   LocalPlayer:GetMouse();

local function Part_Hide()
    if (Toggle_MouseHover == true and Mouse.Target ~= nil) then
        if (ReplicatedStorage:FindFirstChild("[Part_Storage]") == nil) then
            local Model         =   Instance.new ("Model", ReplicatedStorage);
            Model.Name          =   "[Part_Storage]";
            --Mouse.Target:SetAttribute("Source", Mouse.Target:GetFullName())
            Mouse.Target:SetAttribute("Source", Mouse.Target.Name)
            Mouse.Target.Parent =   Model;
        else
            local Model         =   ReplicatedStorage:FindFirstChild("[Part_Storage]");
            --Mouse.Target:SetAttribute("Source", Mouse.Target:GetFullName())
            Mouse.Target:SetAttribute("Source", Mouse.Target.Name)
            Mouse.Target.Parent =   Model;
        end
    end
end

local function Part_Unhide()
    if (ReplicatedStorage:FindFirstChild("[Part_Storage]") ~= nil) then
        local Storage   =   ReplicatedStorage:FindFirstChild("[Part_Storage]");
        for _, Child in pairs (Storage:GetChildren()) do
            --local Source  =   Child:GetAttribute("Source");
            --Child.Parent  =   Source
            Child.Parent    =   Child:GetAttribute("Source");   --  <-- Cause of the issue
        end
    end
end

我不知道属性是否可以接受实例作为值

Child.Parent = Child:GetAttribute("Source");

有没有办法将字符串转换为实例,或者有另一种方法可以将实例另存为值,或者有任何解决此问题的方法吗?如果是这样,不胜感激,提前致谢!

正如您所指出的,实例作为属性类型是不可接受的。

一种解决方法可能是创建一个 ObjectValue,并使用它来保存实例引用。您可以在将其移至 ReplicatedStorage 之前将其填充到对象中,然后在将其从存储中拉出时将其删除。

local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()

-- if the container for part storage doesn't exist, create it
local PartStorage = ReplicatedStorage:FindFirstChild("[Part_Storage]")
if (PartStorage == nil) then
    PartStorage = Instance.new("Model", ReplicatedStorage)
    PartStorage.Name = "[Part_Storage]"
end

local function Part_Hide()
    if (Toggle_MouseHover == true and Mouse.Target ~= nil) then
        -- alias the Mouse Target
        local Part = Mouse.Target
        local PartSource = Part.Parent

        -- move the part to storage
        Part.Parent = PartStorage

        -- hold onto the reference to where this object came from
        local ParentRef = Instance.new("ObjectValue")
        ParentRef.Value = PartSource
        ParentRef.Name = "ParentRef"
        ParentRef.Parent = Part
    end
end

local function Part_Unhide()
    -- move all parts out of storage
    for _, Child in pairs (PartStorage:GetChildren()) do
        -- remove the ObjectValue from the child
        local PartSource = Child:FindFirstChild("ParentRef")
        PartSource.Parent = nil

        -- place the child back into the world
        Child.Parent = PartSource.Value

        -- clean up
        PartSource:Destroy()
    end
end

请注意,除非有意,否则您不应在 LocalScript 中执行此类世界更改操作,因为这些更改只会显示给拥有 LocalScript 的玩家。如果您在服务器端脚本中执行工作,更改将被复制到所有玩家。

我终于成功了,谢谢@Kylaaa

原来你的ObjectValue方法真的有用!我一直在寻找类似的东西,它拥有一个实例的价值,尽管我最终找到了 Roblox 的最新功能并开始关注它(这是我的第一个错误)

第二个错误是我没有使用别名,因为我认为直接使用实例 properties/attributes 与使用别名的效果相同,而别名部分仅用于装饰或仅用于添加一行的东西(方便参考之类的东西)

这是完整的代码:

local function Part_Hide()
    if (Toggle_MouseHover == true and Mouse.Target ~= nil) then
        local Part          =   Mouse.Target;
        local Part_Source   =   Part.Parent;
        local Source        =   Instance.new ("ObjectValue", Part);
        Source.Value        =   Part_Source;
        Source.Name         =   "Source";
        if (ReplicatedStorage:FindFirstChild("[Part_Storage]") == nil) then
            local Model =   Instance.new ("Model", ReplicatedStorage);
            Model.Name  =   "[Part_Storage]";
            Part.Parent =   Model;
        else
            local Model =   ReplicatedStorage:FindFirstChild("[Part_Storage]");
            Part.Parent =   Model;
        end
    end
end

local function Part_Unhide()
    if (ReplicatedStorage:FindFirstChild("[Part_Storage]") ~= nil) then
        local Storage   =   ReplicatedStorage:FindFirstChild("[Part_Storage]");
        for _, Child in pairs (Storage:GetChildren()) do
            local Source    =   Child:FindFirstChild("Source");
            Child.Parent    =   Source.Value;
            Source:Destroy();
            Storage:Destroy();
        end
    end
end