Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I prevent a CharacterAdded loop with custom player models?

Asked by 3 years ago

I'm trying to change players' character models on respawn via player.CharacterAdded, however, when players' character model changes it simply retriggers the function again leading to a loop error to occur (Maximum event re-entrancy depth exceeded for Player.CharacterAdded). How can I make newly respawned players change into a new model with a function without triggering the function to start again?

Here is what I had:

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local oldCharacter = player.Character
        local newCharacter = game.WorkSpace.PreloadedAssets.NewCharacter:Clone()

        newCharacter.HumanoidRootPart.Anchored = false
        newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)

        player.Character = newCharacter
        newCharacter.Parent = game.Workspace
    end)
end)

Answer this question