Occasionally a part doesn't get welded to the player and there's no error

Okay, so when the player joins my game. A weapon is welded to their back like so.

image

Just after I posted this, the problem happened; which is this.

image

Though sometimes it doesn’t get welded/appears and it doesn’t error. This is the code.

 game.Players.PlayerAdded:Connect(function(player) --Server Script
    player.CharacterAdded:Connect(function(character)
	    Weapon.Attach(character, sessionData[player.Name].Stats.Equipped)
    end)
end)


function Weapon.Attach(character, weapon)  --In Module Script
    local item = game.ReplicatedStorage[weapon]:Clone()
    item.Motor6D.Part0 = character.UpperTorso
    item.Motor6D.C0 = CFrame.new(.3,0,.5) * CFrame.Angles(0,0,math.rad(180 - 45))
    item.Parent = character
end

should I just keep calling the function with a repeat until the weapon exists? I feel like this isn’t the best solution. I can’t reliably test to see if this fixes as the problem doesn’t happen much, but it’s still major.

3 Likes

Instance.new"Motor6D" or Instance.new"Weld" is the best way for me. Writes stuff you need, do the parenting last. Rather than placing a Motor6D in the sword before cloning.

note : on phone as usual

Ah okay, thanks. I’ll try that.

I’d also recommend parenting it before you set the joints

1 Like

Is the weapon anchored? If yes - weld won’t work. You need to unanchor it. (Even if you would like to weld anchored thing to character, which should just teleport the character - it won’t work)

It’s not anchored, if it was it wouldn’t work at all. The other reply is probably correct but I haven’t got around to testing.

I found that a small wait() right before welding fixes a lot of problems.

Try adding wait(0.1) before attaching the weapon to the character.

1 Like

I usually use a ManualWeld but and it works just fine.

Also parent first, set properties after --Actually it doesn’t matter, in fact parents last works better lol

Your instinct is correct, it’s not the best solution.

Neither is this. Waits of arbitrary duration for reasons not fully understood are probably best avoided. There are times when waiting for something might be required, particularly with some load order things in order to get Play Solo behavior to match a published game, but it’s still best to only add things to your code if you know why, rather than adding wait() calls until stuff works.

The UpperTorso that exists at CharacterAdded time can be replaced during appearance load, and anything you welded to it will be lost (or can freefall, depending on when exactly it is added relative to character being parented to workspace). You will need either char.ChildAdded to watch for a new UpperTorso, or you’ll need to check on player.CharacterAppearanceLoaded to see if there is a new UpperTorso. Currently, on production, there are a couple of things that can prevent CharacterAppearanceLoaded from firing, such as a player wearing a Content Deleted clothing item, or a random asset load failure, just FYI.

Also, there can be issues in Play Solo with CharacterAdded and CharacterAppearanceLoaded firing with char.Parent still nil, which IIRC doesn’t happen on a normal, published server. You could try waiting for char.Parent to be the workspace, or hook up a char.AncestryChanged handler if needed for Play Solo compatibility, rather than littering the code with wait() statements.

In general, a lot of special stuff happens when a humanoid-equipped character gets parented to the workspace, and you normally want to be explicit about doing things to the character either before or after this point (depending on what you’re doing).

9 Likes

When a part is resized, it breaks connected joints. Maybe the torso is getting resized a moment after spawning. Can you use item.Motor6D.Changed to detect if this is the cause?