Im trying to make gun holding using IKControls, but after a few seconds the weld and tool delete themselves
heres where the script provided is inside the “Gun” tool
local char = game:GetService("Players").LocalPlayer.Character
local hum = char:FindFirstChildOfClass("Humanoid")
script.Parent.Equipped:Connect(function(mouse: Mouse)
for i,v in pairs(char.RightHand:GetChildren()) do
if v.Name == "RightGrip" then
v.Name = "GunGrip"
v.Part0 = hum.RootPart
v.C0 = CFrame.new(1.5, 0, -2) * CFrame.Angles(math.rad(0), math.rad(0), math.rad(0))
end
end
local ikRightArm = Instance.new("IKControl", hum)
ikRightArm.Type = Enum.IKControlType.Transform
ikRightArm.ChainRoot = char.RightUpperArm
ikRightArm.EndEffector = char.RightHand
--local targetLeftArm = Instance.new("Attachment", script.Parent.RootPart)
--targetLeftArm.Name = "TargetLeftArm"
--ikLeftArm.Target = targetLeftArm
ikRightArm.Target = script.Parent.Handle.Target
local poleRightArm = Instance.new("Attachment", hum.RootPart)
poleRightArm.Name = "PoleRightArm"
while task.wait() do
local MiddlePosition = (ikRightArm.EndEffector.Position + ikRightArm.ChainRoot.Position)/2
local PolePosition = (char.UpperTorso.Position + MiddlePosition)/2
poleRightArm.WorldPosition = PolePosition
ikRightArm.Pole = poleRightArm
end
end)
script.Parent.Destroying:Connect(function()
for i,v in pairs(hum:GetChildren()) do
if v.Name == "IKControl" then
v:Destroy()
end
end
for i,v in pairs(hum.RootPart:GetChildren()) do
if v.Name == "PoleRightArm" then
v:Destroy()
end
end
end)
script.Parent.Unequipped:Connect(function()
for i,v in pairs(hum:GetChildren()) do
if v.Name == "IKControl" then
v:Destroy()
end
end
for i,v in pairs(hum.RootPart:GetChildren()) do
if v.Name == "PoleRightArm" then
v:Destroy()
end
end
end)
script.Parent.Equipped:Connect(function(mouse: Mouse)
local char = script.Parent.Parent
local hum = char:FindFirstChildOfClass("Humanoid")
for i,v in pairs(char.RightHand:GetChildren()) do
if v.Name == "RightGrip" then
v.Name = "GunGrip"
v.Part0 = hum.RootPart
v.C0 = CFrame.new(1.5, 0, -2) * CFrame.Angles(math.rad(0), math.rad(0), math.rad(0))
end
end
local ikRightArm = Instance.new("IKControl", hum)
ikRightArm.Type = Enum.IKControlType.Transform
ikRightArm.ChainRoot = char.RightUpperArm
ikRightArm.EndEffector = char.RightHand
--local targetLeftArm = Instance.new("Attachment", script.Parent.RootPart)
--targetLeftArm.Name = "TargetLeftArm"
--ikLeftArm.Target = targetLeftArm
ikRightArm.Target = script.Parent.Handle.Target
local poleRightArm = Instance.new("Attachment", hum.RootPart)
poleRightArm.Name = "PoleRightArm"
while task.wait() do
local MiddlePosition = (ikRightArm.EndEffector.Position + ikRightArm.ChainRoot.Position)/2
local PolePosition = (char.UpperTorso.Position + MiddlePosition)/2
poleRightArm.WorldPosition = PolePosition
ikRightArm.Pole = poleRightArm
end
end)
script.Parent.Destroying:Connect(function()
for i,v in pairs(script.Parent.Parent:FindFirstChildOfClass("Humanoid"):GetChildren()) do
if v.Name == "IKControl" then
v:Destroy()
end
end
for i,v in pairs(script.Parent.Parent:FindFirstChildOfClass("Humanoid").RootPart:GetChildren()) do
if v.Name == "PoleRightArm" then
v:Destroy()
end
end
end)
script.Parent.Unequipped:Connect(function()
for i,v in pairs(script.Parent.Parent:FindFirstChildOfClass("Humanoid"):GetChildren()) do
if v.Name == "IKControl" then
v:Destroy()
end
end
for i,v in pairs(script.Parent.Parent:FindFirstChildOfClass("Humanoid").RootPart:GetChildren()) do
if v.Name == "PoleRightArm" then
v:Destroy()
end
end
end)