Here is the code:
game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) local characterbod = game.ReplicatedStorage.Test:Clone() characterbod.Parent = workspace characterbod:MakeJoints() characterbod.Name = player.Name.."Test" characterbod:MoveTo(workspace.Part.Position) local cam = workspace.CurrentCamera cam.CameraSubject = characterbod.Humanoid cam.CameraType = "Track" player.Character.Parent = nil player.Character = characterbod characterbod.Animate.Disabled = false end) end)
The character is given the body, yet it thinks the character died and repeats the cycle indefinitely.
The hierarchy is fine, but I think the syntax is the problem. I also get a weird error:
07:49:48.195 - maximum event re-entrancy depth exceeded
To test the issue personally, to see what I mean, play it here
Sorry if you seen my script before. I am really desperate for a fix on this, and If anyone can help me, I honestly would appreciate it.
You're lucky you got this problem when you did, because I had exactly the same problem a few weeks ago. First, however, let's look at your LocalScript logic.
PlayerAdded
events don't work well in LocalScripts.
The current behavior is that the event will fire on the client, but only for the client's player, not for other players who enter. -ROBLOX Wiki
You're also using a CharacterAdded
event, but if the local script is in StarterGui it will run every time the player respawns anyways, right? So there's really no need for CharacterAdded
events in LocalScripts.
When I was having a similar problem, I did not use any events because the LocalScript will be cloned into PlayerGui each time a player respawns. When I change the character, however, what is it really doing? It's respawning. This causes the LocalScript to again be cloned into PlayerGui, and then run. When it runs, it creates a new character for the player and sets player.Character
to that new character. Then, since the player's character has again respawned, the LocalScript is again cloned into PlayerGui! Now I have an infinite loop.
Your problem is basically the same, but since you connect the event, instead of the problem being the LocalScript getting cloned again and again into PlayerGui (it will be cloned, but the events won't fire because the player already joined.), your problem is that the event is already connected. When you change the character, what are you doing? You're adding a character. This then causes the CharacterAdded event to fire. When the CharacterAdded
event fires, you add another character. Now you have an infinite loop.
The solution is simple. Use a server script. Then, use a ChildAdded
event on workspace. If the new child added to workspace is the name of the Player, then that means the default character was added to workspace. You can then change the character to your custom character. If the child added to workspace isn't the name of the Player, then do nothing.
function changeChar(plr) local newChr = game.ServerStorage.NewCharacter:Clone() newChr.Parent = workspace newChr:MakeJoints() newChr:MoveTo(Vector3.new(plr.Character.Torso.Position)) plr.Character.Parent = nil plr.Character = newChr end game.Players.PlayerAdded:connect(function(plr) plr.CanLoadCharacterAppearance = false --Prevents hats and such from being loaded. workspace.ChildAdded:connect(function(child) wait(0.3) print(child) if child.Name == plr.Name then changeChar(plr) end end) end)
You will have to use a LocalScript to set the camera, but you can delay it to wait until the new character was given with this simple line of code:
repeat wait() until game.Players.LocalPlayer.Character.Name == "Whatever you're making the name of the new character"
Feel free to ask any questions!