如何检测玩家是否在触摸砖块时佩戴特定 T-Shirt [Roblox - LUA]

How Detect if player wear a specific T-Shirt When he Touch a Brick [Roblox - LUA]

如标题所示,我正在寻找可以做到这一点的脚本。如果有人可以修复此脚本,我会很高兴:D

function onTouched(part)
local h = part.Parent:findFirstChild("Humanoid")
if h ~= nil then
    if player.parent.torso.roblox.Texture == "https://web.roblox.com/Bloxxer-item?id=1028595" then
        script.Parent.Check.Transparency = 0
        wait (2)
        script.Parent.Check.Transparency = 1
    end
end

结束 script.Parent.Touched:连接(onTouched)

如果您找不到任何 free-model 可以满足您的要求或可以编辑;我们开始了:

因为我们经常引用 script.Parent,所以我们做一个 variable:

local Block = script.Parent

还可以通过为其设置变量来避免在代码主体中放置 url 等常量:

local TShirtTexture = "http://www.roblox.com/asset/?version=1&id=1028594"

请注意,T 恤质地 link 与商品 link 不同。我认为 Bloxxer texture is http://www.roblox.com/asset/?version=1&id=1028594. To find the link, join the game in studio with the t-shirt on and inspect T 恤

我们还需要一个debouncer

那我更喜欢 anonymous functions 如果你真的不需要在外面引用它:

Block.Touched:connect(function(Part)
    -- code goes here
end)

part.Parent:findFirstChild 可能不安全,因为 part.Parent 可能是 nil 如果部分在触摸之后但在代码运行之前被删除,所以最好先检查它(一些 VIP 门曾经因为这个原因在你向它们开枪时被打破)。与其他东西一样,检查它是否存在,否则代码可能会在某个时候中断。

接下来,字符Torso被大写。此外,T 恤在角色身上,而不是玩家身上。并加入一些变量

最后一切都在一起了:

-- Put some variables
local Block = script.Parent
local TShirtTexture = "http://www.roblox.com/asset/?version=1&id=1028594"

-- Debounce variable
local Debounce = false

-- Use anonymous function
Block.Touched:connect(function(Part)
    -- Assume that the Part is a BodyPart, thus the parent should be the character
    local Character = Part.Parent

    -- Ensure that our assumption is correct
    if Character == nil or Character:findFirstChild("Humanoid") == nil then return end

    -- Reference to the assumed Torso
    local Torso = Character:findFirstChild("Torso")

    -- Ensure that it exists
    if Torso == nil then return end

    -- Reference to the assumed T-Shirt
    local TShirt = Torso:findFirstChild("roblox")

    -- Ensure that it exists, and that it is a Decal (T-Shirts are decals)
    if TShirt == nil or not TShirt:IsA("Decal") then return end

    -- Ensure the texture is correct
    if TShirt.Texture ~= TShirtTexture then return end

    -- Check debounce
    if Debounce then return end
    Debounce = true

    -- Do stuff
    Block.Check.Transparency = 0
    wait (2)
    Block.Check.Transparency = 1

    Debounce = false
end)

如果您想检查玩家是否拥有该物品,但不必佩戴它,check this out

此外,如果脚本不起作用,记得post相关errors