Only Owner Door

Make a door like in tycoons using Physics Service!

by AnnaPlayGames2008

Author Avatar

Imagine you made a tycoon, and made a only player door that becomes uncollidable for a short time when owner touches it. Your friend plays it and then tells you that a noob slipped into his base while the door was open and started spawn-killing him! How can you make it fully Only owner? Theres a solution to this.

Tutorial

Insert a part that will be our door! Dont forget to anchor it. Now insert a script in it! Insert an ObjectValue in that door. Name it "Owner".

Now, type this in the script:

local owner = script.Parent.Owner
local physics = game:GetService("PhysicsService") --The service that lets you create collision groups.
physics:CreateCollisionGroup("Door") --Create the door collision group.
physics:SetPartCollisionGroup(script.Parent,"Door")
physics:CreateCollisionGroup("OwnerParts") --Create the collision group with the owner character parts so they dont colllide with the door.
physics:CollisionGroupSetCollidable("Door","OwnerParts",false) --Make owner parts and door uncollidable.

script.Parent.Touched:Connect(function(part) --Track when something touches the door.
    local char = part.Parent
    local human = char:FindFirstChildOfClass("Humanoid")
    local plr = game.Players:GetPlayerFromCharacter(char)
    if plr and human then
    if owner.Value == nil then --Check if theres no owner.
            owner.Value = plr
            for i,v in pairs(char:GetDescendants())do
                if v:IsA("BasePart") or v:IsA("Part") then
                    physics:SetPartCollisionGroup(v,"OwnerParts") --Set the bodypart's group to OwnerParts so it doesnt collide with the door!
                end
            end
        else --If there is, then kill the intruder if its not the owner.
            if char ~= owner.Value.Character then
                human:TakeDamage(10000)
                end
        end
        end
end)    

repeat wait() until owner.Value
owner.Value.CharacterAdded:Connect(function(char)
 --Repeat it when the character respawns!
    char.ChildAdded:Connect(function(part)
        if part:IsA("Part") or part:IsA("MeshPart") or part:IsA("BasePart") then
            physics:SetPartCollisionGroup(part,"OwnerParts")
        end
        physics:SetPartCollisionGroup(char:WaitForChild("Head"),"OwnerParts")
        physics:SetPartCollisionGroup(char:WaitForChild("HumanoidRootPart"),"OwnerParts")
    end)
end)

Now you can sleep calmly without thinking about any noob slipping into your base!