Advertisement
DevGabe

Custom Characters Roblox Script

May 26th, 2021
20,390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. -- Custom Characters
  2.  
  3. game.Players.CharacterAutoLoads = false
  4.  
  5. local TeamCharacters = game.ServerStorage:WaitForChild("TeamCharacters")
  6. local StarterCharacterScripts = game.StarterPlayer.StarterCharacterScripts:GetChildren()
  7.  
  8. local NeutralConnections = {} -- TABLES ALWAYS NEED CURLY BRACKETS!
  9. local RespawnTime = 3
  10.  
  11. local Red = game.Teams.Red
  12. local Blue = game.Teams.Blue
  13.  
  14. -- Get Team Spawn Positions (Five Studs Up so they Don't Spawn In The Ground)
  15. local RedSpawnPos = workspace:WaitForChild("RedSpawn").CFrame + Vector3.new(0,5,0)
  16. local BlueSpawnPos = workspace:WaitForChild("BlueSpawn").CFrame + Vector3.new(0,5,0)
  17.  
  18. -------------------------------------------------------------------------------------------
  19.  
  20. -- Spawn As Custom Character
  21. local function OnSpawn(Player)
  22. if not game.Players:FindFirstChild(tostring(Player)) then return end -- Did I Leave Game
  23.  
  24. local FindCharacter = TeamCharacters:FindFirstChild(tostring(Player.Team))
  25. if FindCharacter then -- Found Corresponding Team Character
  26.  
  27. local NewCharacter = FindCharacter:Clone()
  28. NewCharacter.Name = Player.Name
  29.  
  30. -- Get Items in StarterCharacterScripts
  31. for i, item in pairs(StarterCharacterScripts) do
  32. item:Clone().Parent = NewCharacter
  33. end
  34.  
  35. -- Place Character Spawn Position
  36. if Player.Team == Red then
  37. NewCharacter.HumanoidRootPart.CFrame = RedSpawnPos
  38. elseif Player.Team == Blue then
  39. NewCharacter.HumanoidRootPart.CFrame = BlueSpawnPos
  40. end
  41.  
  42. Player.Character = NewCharacter -- I am This Custom Rig
  43. NewCharacter.Parent = workspace -- I am Spawning
  44.  
  45.  
  46. else -- Didn't Find Team Character in Folder
  47.  
  48. -- Spawn As Default Avatar
  49. Player:LoadCharacter()
  50.  
  51. if Player.Neutral then
  52. -- Listen When They Change Teams As They're Neutral
  53. NeutralConnections[Player] = Player:GetPropertyChangedSignal("Team"):Connect(function()
  54. NeutralConnections[Player]:Disconnect()
  55. OnSpawn(Player)
  56. end)
  57. end
  58. end
  59. end
  60.  
  61. -------------------------------------------------------------------------------------------
  62.  
  63. -- Player First Joins Game
  64. local function FirstAdded(Player)
  65.  
  66. -- *** Performance Recommendation:
  67. -- *** Combine Other CharacterAdded Stuff in Here so you Don't Have Like 100 Different Player.CharacterAdded Connections.
  68.  
  69.  
  70. -- Listen When Character Respawns and Dies (Because CharacterAutoLoads = false)
  71. Player.CharacterAdded:Connect(function(Character)
  72.  
  73. local Humanoid = Character:WaitForChild("Humanoid")
  74. Humanoid.Died:Connect(function() -- I am Dead
  75. wait(RespawnTime)
  76. OnSpawn(Player)
  77. end)
  78. end)
  79.  
  80. -- Initial First Spawn
  81. -- *** (You Can Add A Teams Select Starter Screen, Then Call this 'OnSpawn' Using RemoteEvents When The Player Decides Their Team)
  82. OnSpawn(Player)
  83. end
  84. game.Players.PlayerAdded:Connect(FirstAdded)
  85.  
  86. -------------------------------------------------------------------------------------------
  87.  
  88. -- Listen When Player Leaves Team To Change Their Avatar
  89. for i, team in pairs(game.Teams:GetTeams()) do
  90. team.PlayerRemoved:Connect(OnSpawn)
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement