Skip to content

Instantly share code, notes, and snippets.

@Aerodos12
Created January 23, 2018 18:10
Show Gist options
  • Save Aerodos12/9feb9d19ede21eba7848e666b69f7433 to your computer and use it in GitHub Desktop.
Save Aerodos12/9feb9d19ede21eba7848e666b69f7433 to your computer and use it in GitHub Desktop.
Attachments
local Attachment = {}
Attachment.__index = Attachment
local AttachmentStorage = game.ReplicatedStorage.AttachmentStorage
Attachment.AttachOffsets = {};
function Attachment.new(...)
local att = {}
local args = {...}
att.Name = args[1]
att.Title = args[2]
att.Type = args[3] or "Optics"
att.Model = args[4] or AttachmentStorage:FindFirstChild(att.Name)
att.NodeName = args[5] or "SightNode"
att.WeaponName = args[6] or nil;
att.Offset = args[7];
return setmetatable(att,Attachment)
end
function Attachment:SetWeapon(weaponName)
self.WeaponName = weaponName
end
function Attachment.AddWeapon(name)
Attachment.AttachOffsets[name] = {
}
end
function Attachment:Weld(Gun,mainPartName)
if Gun.Name ~= self.WeaponName then return end
local refmodel=self.Model
local menunode = Gun:FindFirstChild(self.NodeName)
local mainpart = Gun:FindFirstChild(mainPartName)
if refmodel then
local model=refmodel:Clone()
local attnode=model.Node
local weldcframes={}
local maincf=attnode.CFrame
local parts=model:GetChildren()
for i=1,#parts do
local v=parts[i]
if v:IsA("BasePart")then
weldcframes[v]= maincf:toObjectSpace(v.CFrame)
end
end
attnode.CFrame=menunode.CFrame
if self.Type=="Optics" then
local del=Gun:GetChildren()
for i=1,#del do
if (del[i].Name=="AimPart" and not del[i]:FindFirstChild("Stay")) then
del[i]:Destroy()
end
end
elseif self.Type=="Laser" then
local del=Gun:GetChildren()
for i=1,#del do
if (del[i].Name=="Laser" and not del[i]:FindFirstChild("Stay")) then
del[i]:Destroy()
end
end
elseif self.Type=="Glowrod" then
local del=Gun:GetChildren()
for i=1,#del do
if (del[i].Name=="Flashlight" and not del[i]:FindFirstChild("Stay")) then
del[i]:Destroy()
end
end
end
for i=1,#parts do
local v=parts[i]
if v:IsA("BasePart")then
if v~=attnode then
local c0=mainpart.CFrame:toObjectSpace(menunode.CFrame) * self.Offset
local weld=Instance.new("Motor6D")
weld.Part0=mainpart
weld.Part1=v
weld.C0=c0*weldcframes[v]
weld.Parent = mainpart
end
v.Anchored=false
v.CanCollide=false
v.Parent=Gun
if v.Name ~= "AimPart" and v.Name ~= "Laser" and v.Name ~= "Flashlight" then
v.Name = self.Name .. "_Part"
end
end
end
attnode:Destroy()
model:Destroy()
end
end
function Attachment.AddAttachmentToWeapon(WName,AttachName,offset)
Attachment.AttachOffsets[AttachName] = offset
end
Attachment.AddWeapon("DLT-20B")
Attachment.AddAttachmentToWeapon("DLT-20B","Reflex",CFrame.new(0,-0.1,-0.46))
return Attachment
local Attachment = require(game.ReplicatedStorage.GunAttachment)
local Attachments = {
["Kobra"] = Attachment.new("Kobra","Kobra Sight","Optics",nil,"SightNode",nil,CFrame.new(0,-0.05,-0.46));
["RifleGlowrod"] = Attachment.new("RifleGlowrod","Rifle Glowrod","Glowrod",nil,"UnderbarrelNode",nil,CFrame.new());
["CarryingHandle"] = Attachment.new("CarryingHandle","A280A1 Carrying Handle","Optics",nil,"SightNode",nil,CFrame.new(0,-0.05,-0.46));
["RedLaser"] = Attachment.new("RedLaser","Red Laser Sight","Laser",nil,"OtherNode",nil,CFrame.new(0,-0.05,-0.46));
["Trijicon"] = Attachment.new("Trijicon","Trijicon Rangefinder","Optics",nil,"SightNode",nil,CFrame.new(0,-0.05,-0.46));
}
return Attachments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment