Hasli4

Untitled

Jul 19th, 2026
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local ServerStorage = game:GetService("ServerStorage")
  4.  
  5. Players.CharacterAutoLoads = false
  6.  
  7. local remote = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("ChooseCharacter")
  8. local charactersFolder = ServerStorage:WaitForChild("Characters")
  9. local characterData = require(ReplicatedStorage:WaitForChild("CharacterData"))
  10.  
  11. local function getSpawnCFrame()
  12. local spawnLocation = workspace:FindFirstChildWhichIsA("SpawnLocation")
  13. if spawnLocation then
  14. return spawnLocation.CFrame + Vector3.new(0, 3, 0)
  15. end
  16.  
  17. return CFrame.new(0, 10, 0)
  18. end
  19.  
  20. local function hideMenu(player)
  21. local playerGui = player:FindFirstChild("PlayerGui")
  22. if not playerGui then
  23. return
  24. end
  25.  
  26. local gui = playerGui:FindFirstChild("CharacterSelectGui")
  27. if gui and gui:IsA("ScreenGui") then
  28. gui.Enabled = false
  29. end
  30. end
  31.  
  32. local function spawnCharacter(player, characterId)
  33. local template = charactersFolder:FindFirstChild(characterId)
  34. if not template then
  35. warn("Не найдена модель персонажа: " .. tostring(characterId))
  36. return
  37. end
  38.  
  39. if player.Character then
  40. player.Character:Destroy()
  41. player.Character = nil
  42. end
  43.  
  44. local character = template:Clone()
  45. character.Name = player.Name
  46.  
  47. local humanoid = character:FindFirstChildOfClass("Humanoid")
  48. local rootPart = character:FindFirstChild("HumanoidRootPart")
  49.  
  50. if not humanoid then
  51. warn("У персонажа " .. characterId .. " нет Humanoid")
  52. character:Destroy()
  53. return
  54. end
  55.  
  56. if not rootPart then
  57. warn("У персонажа " .. characterId .. " нет HumanoidRootPart")
  58. character:Destroy()
  59. return
  60. end
  61.  
  62. character.Parent = workspace
  63. character:PivotTo(getSpawnCFrame())
  64. player.Character = character
  65.  
  66. player:SetAttribute("SelectedCharacter", characterId)
  67. player:SetAttribute("CharacterChosen", true)
  68.  
  69. hideMenu(player)
  70.  
  71. humanoid.Died:Connect(function()
  72. local selected = player:GetAttribute("SelectedCharacter")
  73. if typeof(selected) ~= "string" then
  74. return
  75. end
  76.  
  77. task.delay(2, function()
  78. if player.Parent then
  79. spawnCharacter(player, selected)
  80. end
  81. end)
  82. end)
  83. end
  84.  
  85. Players.PlayerAdded:Connect(function(player)
  86. player:SetAttribute("CharacterChosen", false)
  87. player:SetAttribute("SelectedCharacter", nil)
  88. end)
  89.  
  90. remote.OnServerEvent:Connect(function(player, characterId)
  91. if player:GetAttribute("CharacterChosen") then
  92. return
  93. end
  94.  
  95. if typeof(characterId) ~= "string" then
  96. return
  97. end
  98.  
  99. if not characterData[characterId] then
  100. warn("Игрок попытался выбрать неизвестного персонажа: " .. characterId)
  101. return
  102. end
  103.  
  104. spawnCharacter(player, characterId)
  105. end)
Advertisement
Add Comment
Please, Sign In to add comment