Hasli4

Untitled

Aug 5th, 2026
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 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. print("Server script started")
  8.  
  9. local remote = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("ChooseCharacter")
  10. local characterData = require(ReplicatedStorage:WaitForChild("CharacterData"))
  11. local charactersFolder = ServerStorage:WaitForChild("Characters")
  12. local spawnPoint = workspace:WaitForChild("CharacterSpawnPoint")
  13.  
  14. local function spawnCharacter(player, characterId)
  15. local template = charactersFolder:FindFirstChild(characterId)
  16. if not template then
  17. return false, "Не найдена модель персонажа: " .. tostring(characterId)
  18. end
  19.  
  20. local character = template:Clone()
  21. character.Name = player.Name
  22. character.Parent = workspace
  23.  
  24. local ok, err = pcall(function()
  25. character:PivotTo(spawnPoint.CFrame + Vector3.new(0, 3, 0))
  26. end)
  27.  
  28. if not ok then
  29. character:Destroy()
  30. return false, "Не удалось переместить персонажа: " .. tostring(err)
  31. end
  32.  
  33. player.Character = character
  34. player:SetAttribute("CharacterChosen", true)
  35. player:SetAttribute("SelectedCharacter", characterId)
  36.  
  37. return true
  38. end
  39.  
  40. Players.PlayerAdded:Connect(function(player)
  41. player:SetAttribute("CharacterChosen", false)
  42. player:SetAttribute("SelectedCharacter", "")
  43. end)
  44.  
  45. remote.OnServerEvent:Connect(function(player, characterId)
  46. if player:GetAttribute("CharacterChosen") then
  47. return
  48. end
  49.  
  50. if typeof(characterId) ~= "string" then
  51. remote:FireClient(player, false, "Неверный CharacterId")
  52. return
  53. end
  54.  
  55. if not characterData[characterId] then
  56. remote:FireClient(player, false, "Нет данных в CharacterData для " .. characterId)
  57. return
  58. end
  59.  
  60. local success, errorMessage = spawnCharacter(player, characterId)
  61. if success then
  62. remote:FireClient(player, true)
  63. else
  64. remote:FireClient(player, false, errorMessage)
  65. end
  66. end)
Advertisement
Add Comment
Please, Sign In to add comment