I’m familiar with OOP, but I have not utilized as much as I wanted to in the past.
I know you can make custom events with OOP, but what about just using simple ROBLOX-built in events, like Humanoid.Died
or Players.PlayerAdded
?
For example, I am working on a bounty system for some of my games. I’ve created one(s) before that weren’t very organized. I want to use OOP to make creating/editing/organizing them far more efficient, but I’m not entirely sure how.
bounty = {}
bounty.__index = bounty
function bounty.new(victim, setBy, amount)
local newBounty = {}
setmetatable(newBounty bounty)
newBounty.amount = amount
newBounty.setBy = setBy
newBounty.target = victim
return newBounty
end
return bounty
After that, I am confused on how I should code the following into this:
- Humanoid death detection. Basically, detect when the victim/target dies, and see if there is a ‘creator’ tag (used for pinpointing who killed who) at time of death. If there is, and the killer is NOT who set the bounty, award the amount to the killer.
- Player leaves. If the victim/target leaves, and the player who set the bounty is still in game, return the amount to the person who set the bounty.
- The same would be done if the person who set the bounty leaves the game, the amount would be awarded to the victim/target; bounties would be indefinitely long until one specific party dies by combat of a third party or either party leaves the game.
In other OOP codes I’ve seen, typically functions were placed outside the whatever.new call, but usually these functions would get fired by a third party script, not called from within it? Would I have to throw an event call in the bounty.new
function, and have it point to something called bounty:Death()
or bounty:PlayerLeft()
?
bounty = {}
bounty.__index = bounty
function bounty.new(victim, setBy, amount)
local newBounty = {}
setmetatable(newBounty bounty)
newBounty.amount = amount
newBounty.setBy = setBy
newBounty.target = victim
game.Players.PlayerRemoving:Connect(PlayerLeft)
return newBounty
end
bounty:PlayerLeft(plr)
--- code
end
return bounty