local RunService = game:GetService("RunService")
local GrabModule = {}
--[[
-- This Modular Was By Yours Truly Rin :D
-- Discord ; sn.ws
-- Date : 30/11/2024
-- Last Updated : 30/11/2024
-- Version : 0.1.0
--
-- This Module Was Made To Easily Grab And Weld Any Part To Any Other Part
-- This Module Can Also Be Used To Weld You And Your Enemies For A Faster And
Easier Way To Code
--
-- This Module Is Very Powerful And Can Be Used In Many Ways With 6 Functions
--
-- 1. GrabModule:StartGrab(Character,Enemy,Offset,Angles : Optional)
-- 2. GrabModule:StopGrab(Character, Grab : Optional)
-- 3. GrabModule:DurationGrab(Character,Enemy,Offset,Angles : Optional,
Duration)
-- 4. GrabModule:StartBasePartWeld(Part1,Part2,Offset,Angles : Optional)
-- 5. GrabModule:StopBasePartWeld(Part1)
-- 6. GrabModule:DurationBasePartWeld(Part1,Part2,Offset,Angles : Optional,
Duration)
EXAMPLE : [
local GrabModule = require(Path.To.GrabModule)
local Character = player.Character
local Enemy = hit.Parent -- hit if you were using a hitbox for detection
local Offset = Vector.new(0, 0, -5)
local Angles = CFrame.Angles( 0, -math.rad(-180), 0 ) or remove this to use
the default weld CFrame Angles
local Duration = 5 or Sound.TimeLength -- not recommended to use
Sound.TimeLength
GrabModule:StartGrab(Character,Enemy,Offset)
task.wait(Duration)
GrabModule:StopGrab(Character, Character:FindFirstChild("Grab"))
OR FOR SHORTER USE THEN USE:
GrabModule:DurationGrab(Character,Enemy,Offset, Angles or Nil ,Duration)
BASEPART EXAMPLE : [
local GrabModule = require(Path.To.GrabModule)
local Part1 = workspace.Part1
local Part2 = workspace.Part2
local Offset = Vector.new(0, 0, -5)
local Angles = CFrame.Angles( 0, -math.rad(-180), 0 ) or remove this to use
the default weld CFrame Angles
local Duration = 5 or Sound.TimeLength -- not recommended to use
Sound.TimeLength
GrabModule:StartBasePartWeld(Part1,Part2,Offset,Angles : Optional)
task.wait(Duration)
GrabModule:StopBasePartWeld(Part1)
OR FOR SHORTER USE THEN USE:
GrabModule:DurationBasePartWeld(Part1,Part2,Offset, Angles or Nil ,Duration)
]
]]
--[[
<code>Character</code><br> is the Player That Is Grabbing
<code>Enemy</code><br> is the Enemy That Is Being Grabbed
<code>Offset</code><br> is the Offset From The Enemy's HumanoidRootPart
<code>Duration</code><br> is the Duration of How Long The Grab Will Last
<strong> Note: </strong> if the enemy dies or the enemy is not detected then
the grab will automatically stop
@Arguments : <Model> Character, <Model> Enemy, <Vector3> Offset
]]
function GrabModule:StartGrab(Character: Model, Enemy: Model, Offset: Vector3,
Angles: CFrame)
self.HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
self.EnemyHumanoidRootPart = Enemy:FindFirstChild("HumanoidRootPart")
self.EnemyHumanoid = Enemy:FindFirstChildOfClass("Humanoid")
local DefaultAngles = CFrame.Angles(0, -math.rad(180), 0)
if self.HumanoidRootPart and self.EnemyHumanoidRootPart and
self.EnemyHumanoid then
local Motor6D = Instance.new("Motor6D")
Motor6D.Name = "Grab"
Motor6D.Part0 = self.HumanoidRootPart
Motor6D.Part1 = self.EnemyHumanoidRootPart
Motor6D.C1 = CFrame.new(Offset) * (Angles or DefaultAngles)
Motor6D.Parent = self.HumanoidRootPart
RunService.Heartbeat:Connect(function()
if self.EnemyHumanoid.Health <= 0 then
self:StopGrab(Character, Motor6D)
end
end)
else
return warn("Grab cannot be started: Missing required parts.")
end
end
--[[
<code>Character</code><br> is the Player That Is Grabbing
<code>Grab</code> Will AutoDestroy If Not Specified
<code>Grab</code><br> is the Motor6D That Connects Them Together
]]
function GrabModule:StopGrab( Character : Model, Grab : Motor6D )
self.HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
self.Motor6D = Grab or self.HumanoidRootPart:FindFirstChild("Grab")
if not self.Motor6D then return warn("Weld Or Motor6D Was Not Found") end
self.Motor6D:Destroy()
end
--[[
<code>Character</code><br> is the Player That Is Grabbing
<code>Enemy</code><br> is the Enemy That Is Being Grabbed
<code>Offset</code><br> is the Offset From The Enemy's HumanoidRootPart
<code>Duration</code><br> is the Duration of How Long The Grab Will Last
@Arguments : <Model> Character, <Model> Enemy, <Vector3> Offset, <Number>
Duration
]]
function GrabModule:DurationGrab( Character : Model, Enemy: Model, Offset :
Vector3, Angles : CFrame, Duration : number )
self:StartGrab(Character, Enemy, Offset)
task.wait(Duration)
self:StopGrab(Character)
end
--[[
<code>BasePart1</code> is a BasePart That Will Be Welded To
<code>BasePart2</code>
<code>Offset</code> is the Offset From <code>BasePart2</code>
<code>Angles</code> is the Angles From <code>BasePart2</code>
]]
function GrabModule:StartBasePartWeld( BasePart1 : BasePart, BasePart2 : BasePart,
Offset : Vector, Angles : CFrame )
self.Weld = Instance.new("Weld")
self.Weld.Name = "Weld"
self.Weld.Part0 = BasePart1
self.Weld.Part1 = BasePart2
self.Weld.C1 = CFrame.new(Offset) * (Angles or CFrame.Angles(0, 0, 0))
self.Weld.Parent = BasePart1
end
--[[
<code>BasePart1</code> is the BasePart That Has The Weld
]]
function GrabModule:StopBasePartWeld( BasePart1 : BasePart )
self.Weld = BasePart1:FindFirstChild("Weld")
if self.Weld then
self.Weld:Destroy()
end
end
--[[
<code>BasePart1</code> is a BasePart That Will Be Welded To
<code>BasePart2</code>
<code>Offset</code> is the Offset From <code>BasePart2</code>
<code>Angles</code> is the Angles From <code>BasePart2</code>
<code>Duration</code> is the Duration of How Long The Weld Will Last
]]
function GrabModule:DurationBasePartWeld( BasePart1 : BasePart, BasePart2 :
BasePart, Offset : Vector, Angles : CFrame, Duration : number )
self:StartBasePartWeld( BasePart1, BasePart2, Offset, Angles )
task.wait(Duration)
self:StopBasePartWeld( BasePart1 )
end
return GrabModule