【问题标题】:invalid argument #3 (Instance expected, got string)无效参数 #3(预期实例,得到字符串)
【发布时间】:2021-11-28 15:09:44
【问题描述】:

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

虽然有一个几乎相同,即“无效参数 #2(预期字符串,已获取实例)”,但我尝试寻找反向修复它的方法,但它使用 tostring(),我的问题是关于最新功能:Instance Attributes,它不支持实例作为值,我唯一能做的就是将其转换为字符串,现在问题是关于使用 Adornee/Instance 或其他东西的 Part.Parent我不知道解决方法

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");

有没有办法将字符串转换为实例,或者将实例保存为值的替代方法,或者针对此问题的任何解决方法?如果是这样,感谢您的回答,在此先感谢!

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    正如您所指出的,实例不能作为属性类型。

    一种解决方法可能是创建一个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 的玩家。如果您在服务器端脚本中执行工作,这些更改将复制到所有玩家。

    【讨论】:

    • 还找到了另一种方法,如:Split(),它手动将其转换为实例,将两者都尝试一下,看看哪个有效,谢谢您的回答!
    【解决方案2】:

    我终于成功了,谢谢@Kylaaa

    事实证明,您的 ObjectValue 方法确实有效!我一直在寻找类似的东西,它包含一个实例的值,尽管我最终找到了 Roblox 的最新功能并开始专注于它(这是我的第一个错误)

    第二个错误是我没有使用别名,因为我认为直接使用实例属性/属性与使用别名的效果相同,而别名部分只是用于装饰或只是为一行添加的东西(比如制作方便参考)

    这是完整的代码:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多