如何检测玩家是否穿着特定的 T 恤当他触碰砖块 [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

end script.Parent.Touched:connect(onTouched)

点赞
用户1938640
用户1938640

如果您无法找到想要的或可编辑的免费模型,请看这里:

因为我们经常引用script.Parent,所以让我们先声明变量:

local Block = script.Parent

还需要避免在代码内部放置常量,例如 URL,所以我们也要声明变量:

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

请注意,T 恤贴图链接与物品链接不同。我认为Bloxxer T 恤的链接是 http://www.roblox.com/asset/?version=1&id=1028594。要找到链接,在工作室中穿上 T 恤并检查 T 恤。

我们还需要一个去抖动功能。

如果不需要在其他地方引用,我更喜欢匿名函数

Block.Touched:connect(function(Part)
    -- 代码放在这里
end)

代码中的 part.Parent:findFirstChild 可能不安全,因为如果触摸后但在代码运行前移除该部件,则 part.Parent 可能是 nil,所以最好先检查它(某些 VIP 门以前就会因为这个原因而坏掉)。其他代码也是一样,一定要检查它们是否存在,否则代码可能在某个时刻崩溃。

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

最后,将所有内容结合在一起:

-- 声明一些变量
local Block = script.Parent
local TShirtTexture = "http://www.roblox.com/asset/?version=1&id=1028594"

-- 去抖变量
local Debounce = false

-- 使用匿名函数
Block.Touched:connect(function(Part)
    -- 假设 Part 是 BodyPart,因此父级应是角色
    local Character = Part.Parent

    -- 确保我们的假设是正确的
    if Character == nil or Character:findFirstChild("Humanoid") == nil then return end

    -- 引用假定的 Torso
    local Torso = Character:findFirstChild("Torso")

    -- 确保它存在
    if Torso == nil then return end

    -- 引用假定的 T 恤
    local TShirt = Torso:findFirstChild("roblox")

    -- 确保它存在,并且它是贴花(T 恤是贴花)
    if TShirt == nil or not TShirt:IsA("Decal") then return end

    -- 确定贴图是否正确
    if TShirt.Texture ~= TShirtTexture then return end

    -- 检查去抖动
    if Debounce then return end
    Debounce = true

    -- 做一些事情
    Block.Check.Transparency = 0
    wait (2)
    Block.Check.Transparency = 1

    Debounce = false
end)

如果您想检查玩家是否拥有物品,但不需要穿上它,请查看此处

此外,如果脚本无法正常工作,请记得发布相关的错误

2016-08-20 11:02:25