Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Lobby Door Owner (It won't work when I respawn my character)?

Asked by 2 years ago
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")
            print("RESPAWN")
        end
        physics:SetPartCollisionGroup(char:WaitForChild("Head"),"OwnerParts")
        physics:SetPartCollisionGroup(char:WaitForChild("HumanoidRootPart"),"OwnerParts")
    end)
end)

For some reason, it would not work when I respawn my character. Anyone can tell me why? Specifically the line where it has repeat wait() until owner.Value

Answer this question