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
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!
4 comments
It was a server script in the workspace. But this helps alot. Thanks!
It didn't work. I am about to honestly quit on this...
Well if it was a server script then game.Workspace.CurrentCamera will never work, and there's no reason to put it in ReplicatedStorage instead of ServerStorage. Same problem though, the CharacterAdded event fires over and over. Also, I didn't give you a script to copy and paste, I gave you an example. You will have to edit line 2, for example. I also use the script myself so I know it is functiona
Thansks. I will see to editing this code. I just saw it due to my work on unity.