prevent humanoid spawning off the map?
I am having a problem with respawning a player when it dies. as sometimes the player spawn outside the map like this picture.
I think this problem started when I use ClonedMap:Destroy() after the game ended thus making the player respawning off the map.
is there any script that I can use to prevent the player from respawning off the map after it dies or when the maps changes?
here is the full code.
001 | local PointsService = game:GetService( "PointsService" ) |
002 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
003 | local ServerStorage = game:GetService( "ServerStorage" ) |
004 | local MapsFolder = ServerStorage:WaitForChild( "Maps" ) |
005 | local Status = ReplicatedStorage:WaitForChild( "Status" ) |
006 | local GameLength = 150 |
011 | Status.Value = "waiting for players" |
012 | repeat wait( 1 ) until game.Players.NumPlayers > = 2 |
013 | Status.Value = "intermission" |
017 | for i, player in pairs (game.Players:GetPlayers()) do |
019 | table.insert(plrs,player) |
024 | local AvailableMaps = MapsFolder:GetChildren() |
025 | local ChosenMap = AvailableMaps [ math.random( 1 ,#AvailableMaps) ] |
026 | Status.Value = ChosenMap.Name.. " Chosen" |
028 | local ClonedMap = ChosenMap:Clone() |
029 | ClonedMap.Parent = workspace |
031 | local SpawnPoints = ClonedMap:FindFirstChild( "SpawnPoints" ) |
032 | if not SpawnPoints then |
033 | print ( "spawnpoint not founds!" ) |
036 | local AvailableSpawnPoints = SpawnPoints:GetChildren() |
037 | for i, player in pairs (plrs) do |
039 | character = player.Character |
041 | character:FindFirstChild( "HumanoidRootPart" ).CFrame = AvailableSpawnPoints [ 1 ] .CFrame + Vector 3. new( 0 , 20 , 0 ) |
042 | table.remove(AvailableSpawnPoints, 1 ) |
044 | local Sword = ServerStorage.Sword:Clone() |
045 | Sword.Parent = player.Backpack |
046 | local GameTag = Instance.new( "BoolValue" ) |
047 | GameTag.Name = "GameTag" |
048 | GameTag.Parent = player.Character |
058 | Status.Value = "Get ready to play" |
061 | for i = GameLength, 0 , - 1 do |
063 | for x, player in pairs (plrs) do |
065 | character = player.Character |
066 | if not character then |
069 | if character:FindFirstChild( "GameTag" ) then |
070 | print (player.Name.. " is still in the game!" ) |
073 | print (player.Name.. " has been removed" ) |
078 | print (player.Name.. " has been removed" ) |
081 | Status.Value = "There are " ..i.. " seconds remaining, and " ..#plrs.. " players left" |
083 | Status.Value = "the winner is " ..plrs [ 1 ] .Name |
084 | plrs [ 1 ] .leaderstats.Bucks.Value = plrs [ 1 ] .leaderstats.Bucks.Value + reward |
086 | elseif #plrs = = 0 then |
087 | Status.Value = "nobody won" |
090 | Status.Value = "Time up" |
099 | for i, player in pairs (game.Players:GetPlayers()) do |
100 | character = player.Character |
101 | if not character then |
104 | if character:FindFirstChild( "GameTag" ) then |
105 | character.GameTag:Destroy() |
107 | if player.Backpack:FindFirstChild( "Sword" ) then |
108 | player.Backpack.Sword:Destroy() |
110 | if player.Backpack:FindFirstChild( "Sword" ) then |
111 | character.Sword:Destroy() |
114 | player:LoadCharacter() |
117 | Status.Value = "Game Ended" |