How do you make Ownable Doors?

I am making a gamemode and need to be able to have players own doors like in darkrp. I don’t know where to start so any help is appreciated.

http://www.facepunch.com/showthread.php?t=891329&highlight=own+doors

There are some simple examples here.

Let’s start simple and use console commands, here is the simplest implementation I can think of :

Note : This code must be used serverside
[lua]///////////////////////
//Door detection code//
///////////////////////

local DoorTypes = – A list of all entities we recognize as doors
{

"func_door",
"func_door_rotating",
"prop_door_rotating"

}

function IsADoor(ent) --A function which returns true if we pass it a door entity

for k, v in pairs( DoorTypes ) do

    if ( v == ent:GetClass() ) then return true end

end

return false

end

///////////////////////
// Door Locking //
///////////////////////

–Bind a key to togglelock to lock and unlock doors
concommand.Add(“togglelock”, function(ply,cmd,args) – This console command locks or unlocks the door the player is looking it, if he has the permission

local tr = util.QuickTrace( ply:GetEyePos(), ply:GetAimVector() * 128,  ply  ) -- A trace from the player's eyes to 128 units forward, ignoring the player

if not (tr.Entity and IsADoor(tr.Entity) ) then return end -- If the trace dit not hit an entity or that entity is not a door then stop execution of the code

local door = tr.Entity --Assign the entity found by our trace to the variable door, for convenience

if door.DoorOwner and not (door.DoorOwner == ply)  then -- If the door has a door owner and the player is not the owner then
	ply:ChatPrint("You do not own this door!") -- Warn them
	return -- End execution of the code
end
-- At this point they should be allowed to interact with the door
door:Fire("Toggle", "", 0) -- Fire the toggle input on the door. This will lock or unlock it.

end)

///////////////////////
// Door Owning //
///////////////////////

–Bind a key to toggleown to own and unown doors
concommand.Add(“toggleown”, function(ply,cmd,args)

local tr = util.QuickTrace( ply:GetEyePos(), ply:GetAimVector() * 128,  ply  ) -- A trace from the player's eyes to 128 units forward, ignoring the player

if not (tr.Entity and IsADoor(tr.Entity) ) then return end -- If the trace dit not hit an entity or that entity is not a door then stop execution of the code

local door = tr.Entity --Assign the entity found by our trace to the variable door, for convenience

if door.DoorOwner then -- If the door has a door owner

	if door.DoorOwner == ply then -- and the player is the owner then
		door.DoorOwner = nil -- Erase the current owner, the door is now unowned
	else -- Else if the player did not own the door
		ply:ChatPrint("You do not own this door!") -- Warn them
	end
	
else -- Else if the door has no owner
	door.DoorOwner = ply -- Make the player the new owner, the door is now owned.
end

end)[/lua]
:science:

Now that you have the base you can build on top of it to add things like a User Interface or a requirement to own doors ( like having enough money). Note that I typed this in the reply window so I can not guarantee I didn’t make a typo or something that would break the code. I hope this helps you. :smile:

[release]Useful links :

**[Entity.GetClass

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Entity.GetClass)**
**[Util.QuickTrace

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Util.QuickTrace)**
**[Player.ChatPrint

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Player.ChatPrint)**[/release]