Problem with grip to motor script for tools

My script is a system where it takes a weld and a motor 6d and gets it so the right arm is motor 6d’ed to the tool handle, this script works perfectly fine on roblox studio but when in the actual roblox player this script either:

  • Fails to get the motor6d and the gun goes to tool grip positions (which makes it goes sideways)
  • Tool falls out of the players hand and falls through the map

Example pictures

image

image

Heres the script

local motorName = "Handle" 

local player
local character
local humanoid
local rightHand

script.Parent.Equipped:Connect(function()
	if player == nil then
		player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	end
	if character == nil or character.Parent == nil then
		character = script.Parent.Parent
		humanoid = character.Humanoid
		rightHand = character:WaitForChild("Right Arm")
	end
	local getWeld = rightHand:WaitForChild("RightGrip")      
	local motor = Instance.new("Motor6D")
	motor.Name = motorName
	motor.Part0 = getWeld.Part0
	motor.Part1 = getWeld.Part1
	--motor.C0 = getWeld.C0
	--motor.C1 = getWeld.C1

	motor.Parent = rightHand
	motor.Parent = rightHand

	getWeld:Destroy()

	local function onUnequipped()
		if motor then
			motor:Destroy()
		end
	end
	script.Parent.Unequipped:Connect(onUnequipped)

	local function onParentChanged()
		if not script.Parent:IsDescendantOf(game) then
			onUnequipped()
		end
	end
	script.Parent.AncestryChanged:Connect(onParentChanged)
end)

This has really made my game annyoing and its demoralizing because the amount of times I’ve tried fixing this so any help would be greatly appreciated!