Wierd script problem i guess

I am making a gun
it uses Motor6D to connect the player torso to the gun every time the player equips it.
script(body attach is just something that holds the gun by weld)

serverscript in workspace

local location = game.StarterPack.Pistol.Gun:FindFirstChild("BodyAttach")
	
game.Players.PlayerAdded:Connect(function(plr)			
	plr.CharacterAdded:Connect(function(char)		
		local M6D = Instance.new("Motor6D", char.Torso)
		M6D.Name = "ToolGrip"
		print("ToolGrip Created")
	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)
                               

local script in the tool

local Gun = script.Parent:FindFirstChild("Gun")
local weapon = script.Parent

script.Parent.Equipped:Connect(function()
	game.ReplicatedStorage.ConnectM6D:FireServer(Gun:FindFirstChild("BodyAttach"))
	local char = script.Parent.Parent
	char.Torso.ToolGrip.Part0 = char.Torso
	char.Torso.ToolGrip.Part1 = Gun:FindFirstChild("BodyAttach")
end)

weapon.Unequipped:Connect(function()
	game.ReplicatedStorage:FireServer()
end)

it not connect the gun to the body
when i apply the same script to the other gun it work

What… what is the problem? Any video/photos of it? Atleast an explanation? Just giving a whole code without explaining the problem won‘t help anyone…

local players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")

players.PlayerAdded:Connect(function(plr)			
	plr.CharacterAppearanceLoaded:Connect(function(char)
		local torso = char:FindFirstChild("Torso")
		if torso then
			local M6D = Instance.new("Motor6D")
			M6D.Parent = torso
			M6D.Name = "ToolGrip"
			print("ToolGrip Created")
		end
	end)
end)

rs.ConnectM6D.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local bp = plr.Backpack
	local pistol = bp.Pistol
	local gun = pistol.Gun
	local bodyAttach = gun.BodyAttach
	local torso = char:FindFirstChild("Torso")
	if torso then
		torso.ToolGrip.Part0 = torso
		torso.ToolGrip.Part1 = bodyAttach
	end
end)

rs.DisconnectM6D.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local torso = char:FindFirstChild("Torso")
	if torso then
		torso.ToolGrip.Part1 = nil
	end
end)

Remember that because “Torso” is where the Motor6D instance is parented to this will only work for R6 avatars as for R15 avatars that joint is split into a lower torso and upper torso.

It’s also best to avoid using the parent parameter, more about that here.

I’ve swapped CharacterAdded for CharacterAppearanceLoaded as that fires when the character’s avatar is loaded.

You’re also referencing the tool inside StarterPack, not the tool inside the backpack folder of the player instance (which is where tools are copied to from the StarterPack folder each time the player’s character reloads/spawns etc.).

local weapon = script.Parent
local Gun = weapon:FindFirstChild("Gun")
local rs = game:GetService("ReplicatedStorage")

weapon.Equipped:Connect(function()
	local char = weapon.Parent
	local torso = char:FindFirstChild("Torso")
	if torso then
		rs.ConnectM6D:FireServer()
		torso.ToolGrip.Part0 = torso
		torso.ToolGrip.Part1 = Gun:FindFirstChild("BodyAttach")
	end
end)

weapon.Unequipped:Connect(function()
	local plr = weapon.Parent.Parent
	local char = plr.Character
	local torso = char:FindFirstChild("Torso")
	if torso then
		rs.DisconnectM6D:FireServer()
	end
end)

and the necessary changes to the local script as well.

If you don’t want to use @Forummer’s script for some reason, then the only real issue I see is naming your location variable the same as your argument in your remote event. You really don’t need that variable at all.

It looks like you’re trying to use Headstackk’s animating tutorial (the one I use as well), and everything else looks fine except for that variable.

weapon.Unequipped:Connect(function()
	game.ReplicatedStorage:FireServer()
end)

and yeah, not only is the original value for the variable “location” being overridden by its redeclaration as a parameter and thus the received value the client shouldn’t be sending data when firing the server anyway, it’s best to just call FireServer() with no arguments.

1 Like

Yep otherwise an exploiter will be holding their gun on their head or something.

It’s only because it’s unnecessary. It’s wrong to go overboard with hack prevention and security. The client should be trusted enough to take care of most functions without sending it all to the server.

I don’t think removing one parameter is going “overboard” on exploit prevention, especially when the thing you’re removing could result in some messed up stuff, but I don’t wanna talk about what is right and what is wrong here.