Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a Player spawn in a certain team spawn when they die?

I have a script where when a player presses a button, they get teleported to their spawn:


local Plr = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.Visible = false
  Plr.Team = game.Teams.Prisoner
  Plr.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.Prisoner.Position + Vector3.new(0, 10, 0))
 workspace.CurrentCamera.CameraType = Enum.CameraType.Custom

end)

I have a spawn location called 'Prisoner' and the team colour is set exactly to the prisoner team colour, with both 'Neutra'l and 'AllowTeamChangeOnTouch' set to false.

However, whenever a player dies, it doesn't respawn it back to their team's spawnlocation, but spawns them in the middle of the map.

How do I fix this problem?

Any help is appreciated.

1 answer

You can use the RespawnLocation property of Player to assign a designated spawn-location of the respective Team.

local Player = game:GetService("Players").LocalPlayer
Player.RespawnLocation = workspace[Player.Team.Name]

Ensure the Instance is a SpawnLocation and is a child of workspace

If you wish to have them respawn at an Instance that is not a SpawnLocation, you can adjust their position manually as in your original Script. To do so when they reload, use the CharacterAdded signal to run the code when their avatar is rebuilt.

You can use a CharacterAdded event to set their position based on the assigned team.

local Player = game:GetService("Players").LocalPlayer

local function PlaceCharacterAtTeam(Character)
    if not (Character) return end
   local TeamSpawn = workspace[Player.Team.Name]
    if (TeamSpawn) then
      Character:MoveTo(TeamSpawn.CFrame.p + Vector3.new(0,3,0))
    end
end

Player.CharacterAdded:Connect(PlaceCharacterAtTeam)

7 comments

0

I typed in this code: Player.Prisoner = workspace[Player.Team.Name] (where Prisoner is the name of the SpawnLocation under workspace. Then Roblox outputs with this message: Unselected is not a valid member of Workspace. How do I fix this problem?

0

When I type in the CharacterAdded event, this message occurs: Model:SetPrimaryCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this. How do I fix this problem? Thank you

0

I find that odd, Player.Prisoner will fail, leave it as Player.RespawnLocation = workspace[Player.Team.Name]

0

It is trying to find a Part in workspace that matches the name of their Team, ensure the SpawnLocation is called Prisoner, as well as team

0

The reason why I couldn’t is because they had no team, make sure the player is a Prisoner before running that line

0

With the modified CharacterAdded code, try it with MoveTo

0

Both codes shows no errors in the Roblox output, but it still spawns my player in the middle of the map.

This discussion is preserved as it was. New answers, comments, and votes are closed.