简体   繁体   English

尝试使用 Name 索引 nil

[英]Attempt to Index nil with Name

Im creating a script using remote functions, but at elseif part2.Name==selected.Name is says: "attempt to index nil with Name" here is the script:我正在使用远程函数创建一个脚本,但是在elseif part2.Name==selected.Name是说:“尝试使用 Name 索引 nil”这里是脚本:

 game.ReplicatedStorage.Events.PlaceBlock.OnServerInvoke= function(player,pos,selected)
        for i,part2 in ipairs(parts:GetChildren()) do
            if part2== nil then
                partPlace= parts.BasicPart
            elseif part2.Name==selected.Name then
                partPlace = selected:Clone()
            end
        end
        partPlace.CFrame = CFrame.new(math.ceil(pos.X), math.ceil(pos.Y)+2, math.ceil(pos.Z))
        partPlace.Orientation = Vector3.new(0,0,0)
        partPlace.Parent = player.Plate.Value.Parts
        partPlace.Owner.Value=player
    end

Your problem is probably, that selected is nil (maybe you forgot to pass a third argument when you called the function, or it was nil).您的问题可能是,selected 是 nil(也许您在调用 function 时忘记传递第三个参数,或者它是 nil)。

There are global variables in your code that are missing.您的代码中缺少全局变量。 This problem has been unsolved for a while.这个问题有一段时间没有解决。 I would suggest though in this scenario that you break the loop once you have found the part in question.不过,在这种情况下,我建议您在找到有问题的部件后打破循环。 There is no need to check for a nil value in the loop, because all Instances from within another Instance are always going to be an Instance, and not nil .没有必要在循环中检查nil值,因为来自另一个 Instance 的所有 Instances 总是一个 Instance,而不是nil

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local PlaceBlock = Events:WaitForChild("PlaceBlock")

function PlaceBlock.OnServerInvoke(player: Player, position: Vector3, selected: BasePart)
    assert(typeof(player) == 'Instance' and player:IsA("Player"), "player must be a Player")
    assert(typeof(position) == 'Vector3', "position must be a Vector3")
    assert(typeof(selected) == 'Instance' and selected:IsA("BasePart"), "selected must be a BasePart")

    local placement

    for _, v in pairs(parts:GetChildren()) do
        if typeof(v) == 'Instance' and v.Name == selected.Name then
            placement = v:Clone()
            break
        end
    end

    if typeof(placement) ~= 'Instance' then
        placement = parts.BasicPart
    end

    placement.CFrame = CFrame.new(math.ceil(pos.X), math.ceil(pos.Y) + 2, math.ceil(pos.Z))
    placement.Orientation = Vector3.new(0, 0, 0)
    placement.Parent = player.Plate.Value.Parts
    placement.Owner.Value = player
end

Why I chose to solve this problem should not really be a concern, this has been left sitting around for quite a while.为什么我选择解决这个问题真的不应该是一个问题,这已经被搁置了很长一段时间。

I hope you've found your solution, regardless, while this solution is here for those who may be curious as to what the answer may be.无论如何,我希望您已经找到了解决方案,而这个解决方案是为那些可能对答案可能是什么感到好奇的人准备的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM