Search code examples
luaroblox

coI want to open a door on player touch if a Value in the players startergui is true


When the player touches a block in front or behind the door I want it to check if a value in the players starter gui is true and if it is then open the door I can do this when a block is clicked but not when a part it touched when I tried. So I tried using what I would use for a clickable brick and just changing it to the touched function but that did not work. code:

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) -- how long the door will stay open
            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)

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) -- how long the door will stay open
            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)

Solution

  • Touch doesn't give you the player. Rather what touched the block, in case of a player the part of the player that touched the part.

    It is recommended you check if it was a player that touched:

    touchedPart.Parent:FindFirstChild("Humanoid")

    Then you can find the player using the player service:

    Players:GetPlayerFromCharacter(touchedPart.Parent)