I have a localscript that basically attaches a gun to the player’s right hand via a motor6d and it all works beautifully in a localscript but when I decided to do the same process for the server replication it all just stopped working and sadly this is not the first time I get stuck on the server side.
Here is the code: (explanation below the code)
local function ReplicateGun(player: Player, state: string, gun: string)
if state == "Create" then
print(gun)
local thirdPersonJoint = Instance.new("Motor6D")
local ThirdPersongunModel = replicatedStorage.Items:FindFirstChild(gun):Clone()
local ThirdPersongunName = gun.Name
local rightHand = player.Character:WaitForChild("RightHand")
thirdPersonJoint.Name = "ServerMotor6D"
ThirdPersongunModel.Parent = rightHand
thirdPersonJoint.Parent = rightHand
thirdPersonJoint.Part0 = rightHand
thirdPersonJoint.Part1 = ThirdPersongunModel:FindFirstChild("ImportantParts"):FindFirstChild("Handle")
thirdPersonJoint.C0 = CFrame.new(0,0,0) * CFrame.Angles(math.rad(0),math.rad(90),math.rad(-90))
else
local thirdpersonGunModel = player.Character:WaitForChild("RightHand")[gun] -- error here
player.Character:WaitForChild("RightHand"):FindFirstChild("ServerMotor6D"):Destroy()
thirdpersonGunModel:Destroy()
end
end
caster.LengthChanged:Connect(onLengthChanged)
caster.RayHit:Connect(onBulletHit)
GetInfoRemote.OnServerEvent:Connect(GetGunInfoFunc)
ReplicateGunRemote.OnServerEvent:Connect(ReplicateGun())
sorry for the code being hard to read, I’m really sorry
basically the code is supposed to create a motor6d, parent the M6D and the gun model to the hand, then adjust the C0 and C1 stuff later down the road. The main error while testing starts at the line after the else statement (local thirdpersonGunModel), it reads attempt to index nil with character. I already tried countless other methods like adding a playerAdded function at the start of the code but it also didn’t work.
I don’t have any other ideas, if anyone knows how to overcome this issue then I would be super grateful, even if not then I’m still super grateful
thank you
else
if player.Character then
local RightHand = player.Character:WaitForChild("RightHand")
local thirdpersonGunModel = RightHand:WaitForChild(gun)
RightHand:FindFirstChild("ServerMotor6D"):Destroy()
thirdpersonGunModel:Destroy()
end
end
local function ReplicateGun(char, state: string, gun: string)
if state == "Create" then
print(gun)
local thirdPersonJoint = Instance.new("Motor6D")
local ThirdPersongunModel = replicatedStorage.Items:FindFirstChild(gun):Clone()
local ThirdPersongunName = gun.Name
local rightHand = char:WaitForChild("RightHand")
thirdPersonJoint.Name = "ServerMotor6D"
ThirdPersongunModel.Parent = rightHand
thirdPersonJoint.Parent = rightHand
thirdPersonJoint.Part0 = rightHand
thirdPersonJoint.Part1 = ThirdPersongunModel:FindFirstChild("ImportantParts"):FindFirstChild("Handle")
thirdPersonJoint.C0 = CFrame.new(0,0,0) * CFrame.Angles(math.rad(0),math.rad(90),math.rad(-90))
else
local thirdpersonGunModel = char:WaitForChild("RightHand")[gun] -- error here
char:WaitForChild("RightHand"):FindFirstChild("ServerMotor6D"):Destroy()
thirdpersonGunModel:Destroy()
end
end
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
ReplicateGun(char...)
end)
end)
this will make the function run every time a players character is loading in, but you need to add the other params to the ReplicateGun call in the character added event
Sorry for the late reply, but it worked! Thank you!
I kind of tweaked it a bit because for me if I connected the function outside of the remoteEvent then it wouldn’t work, so I instead created a direct function inside of the remoteEvent. Here is the code for anyone who might have the same problem:
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
ReplicateGunRemote.OnServerEvent:Connect(function(plr, state: string, gun: string)
if state == "Create" then
print(gun)
local thirdPersonJoint = Instance.new("Motor6D")
local ThirdPersongunModel = replicatedStorage.Items:FindFirstChild(gun):Clone()
local ThirdPersongunName = gun
local rightHand = char:WaitForChild("RightHand")
thirdPersonJoint.Name = "ServerMotor6D"
ThirdPersongunModel.Parent = rightHand
thirdPersonJoint.Parent = rightHand
thirdPersonJoint.Part0 = rightHand
thirdPersonJoint.Part1 = ThirdPersongunModel:FindFirstChild("ImportantParts"):FindFirstChild("Handle")
thirdPersonJoint.C0 = CFrame.new(0,0,0) * CFrame.Angles(math.rad(0),math.rad(90),math.rad(-90))
else
print("destroyed gun on server")
local thirdpersonGunModel = char:WaitForChild("RightHand")[gun] -- error here
char:WaitForChild("RightHand"):FindFirstChild("ServerMotor6D"):Destroy()
thirdpersonGunModel:Destroy()
end
end)
end)
end)