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

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

虽然有一个几乎相同的问题,即“参数无效#2(期望字符串,得到实例)”,但我试图用相反的方法修复它,但它使用了tostring(),而我的问题是关于最新功能:实例属性,它不支持实例作为值,我唯一能做的就是将其转换为字符串,现在问题是关于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");   --  <--问题的原因
        end
    end
end

我不知道是否有一种方法使属性接受实例作为值

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

是否有一种将字符串转换为实例的方法,或将实例保存为值的替代方法,或任何解决此问题的解决方法?如果有,答案将不胜感激,谢谢!

原文链接 https://stackoverflow.com/questions/69499564

点赞
stackoverflow用户2860267
stackoverflow用户2860267

正如你所指出的那样,实例不适合作为属性类型。

一种解决方法可能是创建一个ObjectValue,并使用它来保存实例的引用。您可以将其放入对象中,然后将其移动到ReplicatedStorage中,然后在从存储中获取时删除它。

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

-- 如果零件存储容器不存在,请创建它
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
        -- 别名 Mouse Target
        local Part = Mouse.Target
        local PartSource = Part.Parent

        -- 移动零件到存储中
        Part.Parent = PartStorage

        -- 保存到哪里来的引用
        local ParentRef = Instance.new("ObjectValue")
        ParentRef.Value = PartSource
        ParentRef.Name = "ParentRef"
        ParentRef.Parent = Part
    end
end

local function Part_Unhide()
    -- 将所有零件移出存储
    for _, Child in pairs (PartStorage:GetChildren()) do
        -- 从子对象中移除 ObjectValue
        local PartSource = Child:FindFirstChild("ParentRef")
        PartSource.Parent = nil

        -- 将子对象放回世界空间中
        Child.Parent = PartSource.Value

        -- 清理
        PartSource:Destroy()
    end
end

小提示,除非这是有意的,否则不应在 LocalScript 中执行这些类型的影响世界的操作,因为这些变化只会对拥有 LocalScript 的玩家显示。如果您在服务器端脚本中执行工作,那么所有玩家都会复制这些更改。

2021-10-08 18:20:30
stackoverflow用户13869224
stackoverflow用户13869224

我最终成功让它工作了,谢谢@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
2021-10-08 19:52:40