如何在玩家触摸砖时检测玩家是否穿着特定的T恤[Roblox - LUA]

时间:2016-08-20 03:19:56

标签: lua roblox

在标题中,我正在寻找一个可以做到这一点的脚本。如果有人可以修复这个脚本,我会很高兴: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)

1 个答案:

答案 0 :(得分:0)

如果您找不到任何您想要的或可编辑的free-model;我们走了:

由于我们经常引用script.Parent,因此我们制作一个variable

local Block = script.Parent

另外,我们也可以通过为其设置变量来避免在代码体中放置像url这样的常量:

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

请注意,T恤纹理链接与商品链接不同。我认为Bloxxer纹理是http://www.roblox.com/asset/?version=1&id=1028594。要查找链接,请在工作室中加入T恤和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

此外,如果脚本不起作用,请记得发布相关的errors