Alternative to attaching my tool's Motor6D?

Morning everyone,

So, I’ve started work on my game and it uses a gun system where the guns have multiple parts (for more defined animations and etc…). My only issue is the previous method of connecting the Motor6D between the parent part and the tool’s attachment part doesn’t work.

Snippet of Code that I use

Summary

Client-side

script.Parent.Equipped:Connect(function()

	game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.BodyAttach)
	
	char.Torso.ToolGrip.Part0 = char.Torso
	char.Torso.ToolGrip.Part1 = WeaponTool.BodyAttach

end)

WeaponTool.Unequipped:Connect(function()

	game.ReplicatedStorage.DisconnectM6D:FireServer()

end)

Server-side

game.Players.PlayerAdded:Connect(function(plr)			
	plr.CharacterAdded:Connect(function(char)
				
		local M6D = Instance.new("Motor6D", char.Torso)
		M6D.Name = "ToolGrip"
        end)
end)

game.ReplicatedStorage.ConnectM6D.OnServerEvent:Connect(function(plr,location)

      local char = plr.Character
      char.Torso.ToolGrip.Part0 = char.Torso
      char.Torso.ToolGrip.Part1 = location


end)

game.ReplicatedStorage.DisconnectM6D.OnServerEvent:Connect(function(plr)
    plr.Character.Torso.ToolGrip.Part1 = nil
end)

The aforementioned method that I use brings up an error message when you reset your character with the tool equipped.
https://gyazo.com/7c67ea64f917d138699728751e63ebbf
The error basically states it cannot find a “ToolGrip” Motor6D, the motor responsible for connecting my tool and the parent part.

My main question is, is there an alternative way to properly connect the Motor6D? Thanks for the assistance! :cowboy_hat_face:

You should be using WaitForChild on the motor6d, also, do you see it in the explorer window on your character? Also, you need to add sanity checks to remote event connection on the server.

game.ReplicatedStorage.ConnectM6D.OnServerEvent:Connect(function(plr,location)
      local char = plr.Character or plr.CharacterAdded:Wait()
      local toolGrip = char:WaitForChild("Torso"):WaitForChild("ToolGrip")

      toolGrip.Part0 = char.Torso
      toolGrip.Part1 = location
end)

Also, you are using the depreciated method of instancing, set the properties and then parent the motor6d manually.

1 Like

Pretty sure when you reset, all joints in your character are broken.

1 Like

Ooh, I was using a deprecated method? Didn’t even know that, partially cause I just got back into development after a couple of months lol., thanks! I’ll check it out and test it