So I wanted to make a script where when a player joins, if the player has a specific UserId, they will spawn with a specific custom character. The script works perfectly fine when a player first joins the game, but when the player kills their character, the script starts loop spawning the player's character. Can anyone help see if my code has any problem that results in this loop?
local Tetrantyl = game:GetService("ReplicatedStorage"):FindFirstChild("TetraMorph")
function PlayerAdded(player)
if player.UserId == 161506000 then
print("Tetrantyl has spawned.")
end
local function CharacterAdded(Character)
if player.UserId == 161506000 then
local PlayerAvatar = Tetrantyl:Clone()
PlayerAvatar.Name = "StarterCharacter"
PlayerAvatar.Parent = game:GetService("StarterPlayer")
wait(.1)
player:LoadCharacter()
end
end
CharacterAdded(player.Character or player.CharacterAdded:wait())
player.CharacterAdded:Connect(CharacterAdded)
end
game.Players.PlayerAdded:Connect(PlayerAdded)
for _,player in pairs(game.Players:GetPlayers())do
spawn(function() PlayerAdded(player) end)
end
