当玩家触碰时,我想要打开一个门,如果玩家起始界面中的某个值为 true

当玩家触碰门前或门后的方块时,我想要检查玩家开始界面中的某个值是否为 true,并且如果是,则打开门,当我尝试触碰部件时,我可以做到这一点,但不是当块被点击时。 所以我尝试使用我用于可点击的砖块的内容,并将其更改为触碰函数,但这没有起作用。 代码:

local db = false
local open = script.Parent.Open
local ind = script.Parent.Door1

script.Parent.Trigger.Touched:connect(function(player)
    if db == false then
        db = true
        if player:WaitForChild("PlayerGui").keyCard.Key.Value == true then
            if open.Value == false then
                script.Parent.Sound:Play()
                local f = script.Parent.Door1.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(90),0)
                local f2 = script.Parent.Door2.PrimaryPart.CFrame*CFrame.Angles(0,-math.rad(90),0)
                for i = 0,1,0.1 do
                    local cfm = script.Parent.Door1.PrimaryPart.CFrame:lerp(f,i)
                    local cfm2 = script.Parent.Door2.PrimaryPart.CFrame:lerp(f2,i)
                    script.Parent.Door1:SetPrimaryPartCFrame(cfm)
                    script.Parent.Door2:SetPrimaryPartCFrame(cfm2)
                    wait()
                end
                open.Value = true
                wait(1) -- 门打开的时间
                local f3 = script.Parent.Door1.PrimaryPart.CFrame*CFrame.Angles(0,-math.rad(90),0)
                local f4 = script.Parent.Door2.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(90),0)
                for i = 0,1,0.1 do
                    local cfm = script.Parent.Door1.PrimaryPart.CFrame:lerp(f3,i)
                    local cfm2 = script.Parent.Door2.PrimaryPart.CFrame:lerp(f4,i)
                    script.Parent.Door1:SetPrimaryPartCFrame(cfm)
                    script.Parent.Door2:SetPrimaryPartCFrame(cfm2)
                    wait()
                end
                open.Value = false
            end
        else
            print("Negative")
        end
        db=false
    end

end)

end)

script.Parent.Trigger2.Touched:connect(function(player)
    if db == false then
        db = true
        if player:WaitForChild("PlayerGui").keyCard.Key.Value == true then
            if open.Value == false then
                script.Parent.Sound:Play()
                local f = script.Parent.Door1.PrimaryPart.CFrame*CFrame.Angles(0,-math.rad(90),0)
                local f2 = script.Parent.Door2.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(90),0)
                for i = 0,1,0.1 do
                    local cfm = script.Parent.Door1.PrimaryPart.CFrame:lerp(f,i)
                    local cfm2 = script.Parent.Door2.PrimaryPart.CFrame:lerp(f2,i)
                    script.Parent.Door1:SetPrimaryPartCFrame(cfm)
                    script.Parent.Door2:SetPrimaryPartCFrame(cfm2)
                    wait()
                end
                open.Value = true
                wait(1) -- 门打开的时间
                local f3 = script.Parent.Door1.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(90),0)
                local f4 = script.Parent.Door2.PrimaryPart.CFrame*CFrame.Angles(0,-math.rad(90),0)
                for i = 0,1,0.1 do
                    local cfm = script.Parent.Door1.PrimaryPart.CFrame:lerp(f3,i)
                    local cfm2 = script.Parent.Door2.PrimaryPart.CFrame:lerp(f4,i)
                    script.Parent.Door1:SetPrimaryPartCFrame(cfm)
                    script.Parent.Door2:SetPrimaryPartCFrame(cfm2)
                    wait()
                end
                open.Value = false
            end
        else
            print("Negative")
        end
        db=false
    end
end)
点赞
用户289023
用户289023

Touch 并不会直接获得玩家。相反,它获取的是碰撞的物体,若是玩家与物体发生碰撞,则是玩家身体的部分与该物体发生碰撞。

建议检查触碰的对象是否为玩家:

touchedPart.Parent:FindFirstChild("Humanoid")

之后,可以利用玩家服务来获取玩家信息:

Players:GetPlayerFromCharacter(touchedPart.Parent)
2020-08-07 12:58:05