I made the OwnerDoor script. When someone touches Ownerpart, the owner will be identified. And if you touch the other part (Killpart) and you are’nt owner, you die. But there must be an efficient way to do this with OOP. I want to be sure. code:
--Module script
local Ownerdoor = {}
Ownerdoor.__index = Ownerdoor
function Ownerdoor.New(part,killpart)
local self = setmetatable({},Ownerdoor)
self.Owner = nil
self.Ownerpart = part
self.Killpart = killpart
return self
end
function Ownerdoor:Kill()
local killpart = self.Killpart
local Owner = self.Owner
killpart.Touched:Connect(function(hit)
Owner = self.Owner
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player == nil then return end
local Character = player.Character
local Humanoid = Character and Character:WaitForChild("Humanoid")
if Owner == nil or Owner.Character ~= nil and Humanoid and Character ~= Owner.Character then
Humanoid:TakeDamage(100)
end
end)
end
return Ownerdoor
--Server script
local Ownerdoor = require(game:GetService("ServerScriptService").Ownerdoor)
local killpart = game.Workspace.OwnerDoor.Killpart
local Ownerpart = game.Workspace.Part
local Ownerdoorself = Ownerdoor.New(Ownerpart,killpart)
local con
con = Ownerpart.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
Ownerdoorself.Owner = player
if Ownerdoorself.Owner ~= nil then
con:Disconnect()
end
end)
Ownerdoorself:Kill()
Your code seems to be working as intended, and you are using object-oriented programming (OOP) concepts in a functional manner by creating an instance of the Ownerdoor object. To improve efficiency and maintain code readability, you may consider the following modifications:
Use the local variable self inside functions instead of Ownerdoorself .
Add a Player property to the Ownerdoor object, and update the value inside the Touched event connection.
Make the Kill method a private method, by using a local function and referencing it in the Touched event connection.
Here’s the updated code:
--Module script
local Ownerdoor = {}
Ownerdoor.__index = Ownerdoor
function Ownerdoor.new(part, killpart)
local self = setmetatable({}, Ownerdoor)
self.Ownerpart = part
self.Killpart = killpart
self.Player = nil
return self
end
function Ownerdoor:connect()
local self = self
local killpart = self.Killpart
local function kill(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player == nil then return end
if player ~= self.Player then
local humanoid = player.Character:WaitForChild("Humanoid")
humanoid:TakeDamage(100)
end
end
killpart.Touched:Connect(kill)
end
return Ownerdoor
--Server script
local Ownerdoor = require(game:GetService("ServerScriptService").Ownerdoor)
local killpart = game.Workspace.OwnerDoor.Killpart
local Ownerpart = game.Workspace.Part
local ownerdoor = Ownerdoor.new(Ownerpart, killpart)
local function onTouched(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
ownerdoor.Player = player
end
Ownerpart.Touched:Connect(onTouched)
ownerdoor:connect()