检查玩家是否在 roblox 中拥有工具的副本

Check if player has a duplicate of a tool in roblox

我正在尝试检查玩家的背包中是否有 2 瓶 Bloxy Colas 如果有 2 个,则摧毁另一个

我在脚本中尝试了这段代码,但没有成功

local tmpTable = {} -- store items names

for I,v in pairs(player.Backpack:GetChildren()) do --  loop through all items found
    if  tmpTable [v.Name] ~= nil then -- checks if item exists in the list
        print("a duplicate has been found") -- found item e.g. a duplicate
        v:Destroy() -- deletes tool 
    else
        tmpTable [v.Name]  = "BloxyCola"  -- we don't need to use the value we are only using the key
        print("item added to list") 
    end 
end

The duplicate not being removed. Nothing prints

您在条件语句的两个分支中都打印了一些内容。如果没有打印任何内容,则表示循环不会 运行 一次。这意味着 player.Backpack:GetChildren() return 是一个空的 table。

因为 Instance:GetChildren() 总是 return 一个 table,看到一个空的 table 意味着 Backpack 没有任何 children。