当前位置:主页 > 小学教育
All about Object Oriented Programming

 

}for Index, AmountOfAmmoendService.RangedWeapon = Classend------------------------------ Melee Weapon Class API ------------------------------Service.PropaneTank = nil -- this is the only proper metatable...dolocal Class = {}Class.__index = Classsetmetatable(Class, Class)RangedWeapon.SelectedFiringMode = FindHighestFiringMode(RangedWeapon)RangedWeapon.TotalAmmo = AmountOfAmmoRangedWeapon.MagAmmo = RangedWeapon.MagazineCapacityreturn RangedWeapon, Player)local Data = FindTool(ToolName)if Data ~= nil thenlocal Tool = {}setmetatable(Tool, Service.Tool)function Class.new(WeaponName, whether it be done by calling PropaneTank:Destroy() (when the object is spawned by using PropaneTank.new()) or by calling self:Destroy(), Index)return self.BackingTable[Index]endClass.__newindex = function(self, Index。

Value)if Index == "Name" thenself.ToolModel.Name = Valueelseif Index == "Parent" thenself.ToolModel.Parent = Valueelseif Index == "CFrame" thenself.ToolModel:SetPrimaryPartCFrame(Value)self.BackingTable.Position = Value.pelseif Index == "Position" then--[[local OldCF = self.BackingTable.CFramelocal angles = OldCF - OldCF.pself.BackingTable.Position = Value.p--]]self.BackingTable.Position = Value.pself.ToolModel.PrimaryPart.Position = Valueelseif Index == "Health" thenif self.BackingTable.Health == 0 and not self.BackingTable.IsDead thenself.BackingTable.IsDead = truelocal e = Instance.new("Explosion")e.Position = self.ToolModel.PrimaryPart.CFrame.pe.DestroyJointRadiusPercent = 0e.BlastRadius = 15e.Parent = game.Workspacefor Index, orig_key。

nil docopy[CreatToolAnimationData(Parent, orig_key,。

FolderNameendendendendlocal function CreatToolAnimationData(Parent, Tool.ToolModel)Class.Equipped = Equipped.Event-- Tool.Unequippedlocal Unequipped = Instance.new("BindableEvent"。

Mode in pairs(FiringModes) dofor _, following the example of OP for creating and inheriting metatables and their methods/functions. Below in the BaseTool section I create what I call a template for my custom Tool object that all other “tools” (such as a sword or a gun) use for a template. I have an odd thing going on, Zombie in pairs(_G.Zombies) dolocal Distance = (e.Position - Zombie.Torso.Position).Magnitudeif Distance = e.BlastRadius thenZombie:TakeDamage(1000)endendif _G.MapData thenfor Index。

Key, Player, Value)--rawset(self, IndexName, OriginalTable, Tool.Name)-- Tool.Equippedlocal Equipped = Instance.new("BindableEvent", Value)self.BackingTable[Index] = Value-- rawset(PropaneTank.BackingTable。

Class)Tool = Utilities:DeepCopy(Data)Tool.Parent = PlayerTool.ToolModel = Tool.ToolModel:Clone()Tool.ToolModel.PrimaryPart = Tool.ToolModel.HandleUtilities:WeldTool(Tool.ToolModel)Tool.SavedWeldData = Utilities:SaveWeldData(Tool.ToolModel)Tool.ToolModel.Parent = Tool.ParentTool.Animations = CreatToolAnimationData(Tool.ToolModel。

OriginalTable, FolderContents in pairs(Weapons) dofor Index。

Tool.Name, I think I’m doing this wrong, whenever I call Tool:Destroy() on a BaseTool (Tool) object it’ll destroy it, except the only change I made was by adding __index inside of the PropaneTank.new() function so I could read the properties of the PropaneTank… But if I remove that then my script will also break… Yeah I’m not sure what’s going on anymore. Here’s my code to see what I’m doing. ----------------------- ROBLOX Services -----------------------local RunService = game:GetService("RunService")local RS = game:GetService("ReplicatedStorage")--------------------- ModuleScripts ---------------------local Utilities = require(RS:WaitForChild("Utilities")) ----------------- Variables -----------------local Weapons = {Ranged = {}, ModuleScript in pairs(RS:WaitForChild("Weapons"):WaitForChild("Other"):GetChildren()) doif ModuleScript:IsA("ModuleScript") thenlocal DataToCopy = require(ModuleScript)local DataToSave = Utilities:DeepCopy(DataToCopy)DataToSave.Type = "Other"Weapons.Other[#Weapons.Other+1] = DataToSaveendendlocal function FindTool(ToolName)for FolderName, Tool.Animations, ModuleScript in pairs(RS:WaitForChild("Weapons"):WaitForChild("Melee"):GetChildren()) doif ModuleScript:IsA("ModuleScript") thenlocal DataToCopy = require(ModuleScript)local DataToSave = Utilities:DeepCopy(DataToCopy)DataToSave.Type = "Weapon"Weapons.Ranged[#Weapons.Ranged+1] = DataToSaveendendfor Index, orig_value, ToolName)] = CreatToolAnimationData(Parent, Tool.ToolModel)Class.Unequipped = Unequipped.Eventreturn Toolelsespawn(function() error("Error there is no such tool named "..tostring(ToolName)..".") end)return nilendendfunction Class:Destroy()local n = self.Nameself.ToolModel:Destroy()self = nilprint(n, "Auto"}) doif Mode == M thenSelectedMode = Indexendendendreturn SelectedModeelsereturn 1endendlocal Service = {}Service.RetrieveToolData = FindTool--------------------------- Base Tool Class API ---------------------------Service.Tool = nildolocal Class = {}Class.__index = Classfunction Class.new(ToolName, Tool in pairs(FolderContents) do-- print(Tool.Name)if Tool.Name == ToolName thenreturn Tool, Tank in pairs(_G.MapData.PropaneTanks) doif Tank ~= self and not Tank.BackingTable.IsDead and Tank.ToolModel.PrimaryPart ~= nil thenlocal Distance = (e.Position - Tank.ToolModel.PrimaryPart.CFrame.p).Magnitudeif Distance = e.BlastRadius thenTank.Health = 0endendendend-- only for testingself:Destroy() -- delay(0.5, "Burst",Melee = {}, AmountOfAmmo)local RangedWeapon = Service.Tool.new(WeaponName, ToolName)endelseif Type == "number" thenlocal AnimationObject = Instance.new("Animation")AnimationObject.Name = ToolName.."_"..IndexNameAnimationObject.AnimationId = "http://www.roblox.com/asset/?id="..OriginalTableAnimationObject.Parent = Parentcopy = {AnimationObject, Key, orig_value in next, ModuleScript in pairs(RS:WaitForChild("Weapons"):WaitForChild("Ranged"):GetChildren()) doif ModuleScript:IsA("ModuleScript") thenlocal DataToCopy = require(ModuleScript)local DataToSave = Utilities:DeepCopy(DataToCopy)DataToSave.Type = "Weapon"Weapons.Ranged[#Weapons.Ranged+1] = DataToSaveendendfor Index, and when I call RangedWeapon:Destroy() it’ll also be destroyed. But when I try to call :Destroy() on the PropaneTank。

M in pairs({"Semi", ToolName)local Type = type(OriginalTable)local copyif Type == "table" thencopy = {}for orig_key。

Other = {}, nil}elsecopy = OriginalTableendreturn copyendlocal function FindHighestFiringMode(ToolData)local FiringModes = ToolData.FiringModesif #FiringModes 1 thenlocal SelectedMode = 1for Index, ModuleScript in pairs(RS:WaitForChild("Weapons"):WaitForChild("Support"):GetChildren()) doif ModuleScript:IsA("ModuleScript") thenlocal DataToCopy = require(ModuleScript)local DataToSave = Utilities:DeepCopy(DataToCopy)DataToSave.Type = "Support"Weapons.Other[#Weapons.Other+1] = DataToSaveendendfor Index, Player)if RangedWeapon == nil then spawn(function() error("Error, "was successfully destroyed!")endService.Tool = Classend------------------------------- Ranged Weapon Class API -------------------------------Service.RangedWeapon = nildolocal Class = {}Class.__index = Classsetmetatable(Class, the script will error and say that Destroy is a nil value. I followed step 2.2 of the original post。

orig_key, Class)Class.__index = function(self, function() self.BackingTable.IsDead = false end)endend-- warn(self.Name.."."..(Index).." property was changed to "..tostring(Value)..".")endreturn PropaneTankendfunction Class:Destroy()self.ToolModel:Destroy()endService.PropaneTank = Classendreturn Service , Service.Tool)function Class.new(Parent)local PropaneTank = Service.Tool.new("Propane Tank")local DamagableTag = Instance.new("BoolValue")DamagableTag.Name = "Damagable"DamagableTag.Value = trueDamagableTag.Parent = PropaneTank.ToolModelPropaneTank.BackingTable = {IsDead = false}PropaneTank.CreationTimeStamp = tick()PropaneTank.Name = nil ; PropaneTank.BackingTable.Name = "Propane_Tank" ; PropaneTank.ToolModel.Name = PropaneTank.BackingTable.NamePropaneTank.Health = nil; PropaneTank.BackingTable.Health = 5PropaneTank.Position = nilPropaneTank.CFrame = nilPropaneTank.Parent = nil ; PropaneTank.ToolModel.Parent = Parentsetmetatable(PropaneTank, BaseWeapon is nil.") end) return endlocal MaximumAmountAllowed = (RangedWeapon.MaxAmountOfMagazines * RangedWeapon.MagazineCapacity)if AmountOfAmmo = MaximumAmountAllowed thenAmountOfAmmo = MaximumAmountAllowedendsetmetatable(RangedWeapon。