This code is used to weld the tool the same way roblox default tools weld to the arm of a character
Now you can put this script in a local script to make it weld to the player without lag
local tool=script.Parent
local handle=tool:FindFirstChild("Handle")
--these here are to reduce indexxing and this is a super-micro-optimsation
local instancenew=Instance.new
local mathrad=math.rad
local cframeangles=CFrame.Angles
local cframenew=CFrame.new
local mathhuge=math.huge
--
local function GetClosestPart(m: Instance,from: Vector3): BasePart
local dist=mathhuge
local t=nil
for i, v in m:GetChildren() do
if v:IsA("BasePart") then
local d=(v.Position-from).Magnitude
if d<dist then
dist=d
t=v
end
end
end
return t
end
tool.Equipped:Connect(function()
local ClosestPart=nil
if not tool:FindFirstChild("Handle") then
handle=GetClosestPart(tool,tool:GetPivot().Position)
end
ClosestPart=GetClosestPart(tool.Parent,handle.Position)
for i, v in ClosestPart:GetChildren() do --no need for pairs() because this is luau
local name=v.Name
if name=="Grip" or name=="RightGrip" then
v:Destroy()
end
end
local Weld=instancenew("Weld",ClosestPart)
Weld.Part0=ClosestPart
Weld.Part1=handle
Weld.C0=cframenew(0,-1,0)*cframeangles(mathrad(-90),0,0)
Weld.C1=tool.Grip
Weld.Name="Grip"
end)
This also fixes some bugs where the tool wont even weld at all
This does exactly what roblox tools do when you equip them
It makes the same weld as the “RightGrip” made when equipping a tool but it is just a peice of code that you can copy/paste
The other cool thing about this code is that you can make a tool weld onto the players hand with other parts other than the “Handle”
It takes the default C0 of a rightgrip weld and add the offset set by the tool (tool.grip) into C1
I havent tested it on r15