How to change a players respawn location

the title isnt really what it sounds like

hey developers, i have been working on a project where you can change your team with a textbutton, then when you press play, it kills you, then i hoped it would of respawned you at the selected teams location.

textbutton code:

--local script

script.Parent.MouseEnter:Connect(function()
	script.Parent.Font = Enum.Font.SourceSans
	script.Parent:TweenSize(UDim2.new(0, 265,0, 60), 'InOut', 'Sine', .3, true)
end)

script.Parent.MouseLeave:Connect(function()
	script.Parent.Font = Enum.Font.SourceSansLight
	script.Parent:TweenSize(UDim2.new(0, 265,0, 50), 'InOut', 'Sine', .3, true)
end)

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:WaitForChild('Humanoid')
	
	if player.TeamColor == BrickColor.new('Really black') then
		script.Parent.TextLabel.Text = 'Please select a team to play!'
		wait(2)
		script.Parent.TextLabel.Text = 'Hop on into the gameplay!'
	else
		humanoid.Health = humanoid.Health - humanoid.MaxHealth
		script.Parent.TextLabel.Text = 'Preparing your gameplay..'
		wait(3)
		script.Parent.Parent.Parent.HFrame:TweenPosition(UDim2.new(0,0,0,0), 'InOut', 'Sine', .6)
		wait(1)
		script.Parent.Parent.Visible = false
		wait(1)
		script.Parent.Parent.Parent.HFrame:TweenPosition(UDim2.new(0,0,1,0), 'InOut', 'Sine', .6)
	end
end)

change team script:

--local script

script.Parent.MouseEnter:Connect(function()
	script.Parent.Font = Enum.Font.SourceSans
	script.Parent:TweenSize(UDim2.new(0, 265,0, 60), 'InOut', 'Sine', .3, true)
end)

script.Parent.MouseLeave:Connect(function()
	script.Parent.Font = Enum.Font.SourceSansLight
	script.Parent:TweenSize(UDim2.new(0, 265,0, 50), 'InOut', 'Sine', .3, true)
end)

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:WaitForChild('Humanoid')
	
	player.TeamColor = BrickColor.Black()
end)

i’ve tried respawnlocation, nothing seemed to work.

any suggestions, tips or help would be greatly appreciated,
thanks in advance

1 Like

What you could do is use a remote event to fire the server the player’s team. On the client, you can have all of your team text buttons in a frame, and loop through the buttons. Then when a specific button is clicked you can fire the server the button’s name. On the server you simple receive the remote event and give the player the corresponding team. This is also handy because you can use LoadCharacter() on the server.

For example:

Button A’s name is TeamBlue
Button B’s name is TeamRed

When you click Button B you’re sending over the button’s name so in this case TeamRed to the server.

On the server, you get the team name from the player, and load the character. This will mean that the player will respawn at the corresponding spawn, however the spawn TeamColor(a property of the spawn) has to match your Team color.

Simplified code, you may want to change it to loop through the buttons or ect.

local teams = game:GetService("Teams")

local TeamEvent = game.ReplicatedStorage.RemoteEvent

local team1 = script.Parent.Frame.Team1
local team2 = script.Parent.Frame.Team2

team1.MouseButton1Click:Connect(function()
TeamEvent:FireServer(team1.Name)
print("fired")
end)

team2.MouseButton1Click:Connect(function()
TeamEvent:FireServer(team2.Name)
print("fired")
end)

local teams = game:GetService("Teams")
local TeamEvent = game.ReplicatedStorage.RemoteEvent

TeamEvent.OnServerEvent:Connect(function(Player, TeamName)
	
	if teams:FindFirstChild(TeamName) then
		Player.Team = teams[TeamName]
		Player:LoadCharacter()
		print("Team Changed Successful")
	end
end)
2 Likes

problem is that some exploiter might change those arguments which he doesnt want too!

he can just check on what team he is on then change his team to the other team

i want to try and stay away from all remoteevents (unless i have to use them), can you elaborate on

he can just check on what team he is on then change his team to the other team

i want their respawnpoint to change

1 Like

use the Player.RespawnLocation property to change the Player’s RespawnLocation.

1 Like

as i stated in my post, i tried that, and even if it did work, i’d have to set the location for all the spawns, which would be inefficient

1 Like

you should touch with this inside the spawnpoints property

1 Like

Could you tell what you mean by that

1 Like

if player.TeamColor = BrickColor.new('TeamColour') then
  player.RespawnLocation = workspace.['SpawnName']
elseif player.TeamColor - BrickColor.new('AnotherTeamColour') then

--etc

end

(i’ve never used it before, so i could be wrong, its just a brief guess)

1 Like

Roblox automatically handles RespawnLocations for Teams.
@Xx_FROSTBITExX’s answer is perfect.

You Have to use them in order for the team to change. You can only change Teams on the Server

And Player.RespawnLocation only works if you change it on the server.

2 Likes

it seems to work, but it spawns me at the wrong spawnpoint, any idea why?

tesatrt

--local script
script.Parent.MouseEnter:Connect(function()
	script.Parent.Font = Enum.Font.SourceSans
	script.Parent:TweenSize(UDim2.new(0, 265,0, 60), 'InOut', 'Sine', .3, true)
end)

script.Parent.MouseLeave:Connect(function()
	script.Parent.Font = Enum.Font.SourceSansLight
	script.Parent:TweenSize(UDim2.new(0, 265,0, 50), 'InOut', 'Sine', .3, true)
end)

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:WaitForChild('Humanoid')
	
	if player.TeamColor == BrickColor.new('Really black') then
		script.Parent.TextLabel.Text = 'Please select a team to play!'
		wait(2)
		script.Parent.TextLabel.Text = 'Hop on into the gameplay!'
	elseif player.TeamColor == BrickColor.Black() then
		humanoid.Health = humanoid.Health - humanoid.MaxHealth
		script.Parent.TextLabel.Text = 'Preparing your gameplay..'
		wait(3)
		script.Parent.Parent.Parent.HFrame:TweenPosition(UDim2.new(0,0,0,0), 'InOut', 'Sine', .6)
		wait(1)
		script.Parent.Parent.Visible = false
		wait(1)
		script.Parent.Parent.Parent.HFrame:TweenPosition(UDim2.new(0,0,1,0), 'InOut', 'Sine', .6)
	end
end)
--button script is the same
1 Like

As I said. Team changes only work if it is done on the Server. A RemoteEvent is required

2 Likes

As @LukaDev_0 has mentioned, team changes don’t work on the client, but rather the server. That aside, you aren’t changing the team of the player at all, but rather stating the team colors and tweening your UI’s.

2 Likes
--text button script
script.Parent.MouseEnter:Connect(function()
	script.Parent.Font = Enum.Font.SourceSans
	script.Parent:TweenSize(UDim2.new(0, 265,0, 60), 'InOut', 'Sine', .3, true)
end)

script.Parent.MouseLeave:Connect(function()
	script.Parent.Font = Enum.Font.SourceSansLight
	script.Parent:TweenSize(UDim2.new(0, 265,0, 50), 'InOut', 'Sine', .3, true)
end)

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:WaitForChild('Humanoid')
	
	player.TeamColor = BrickColor.Black()
end)

as stated in the first post

1 Like
--local script
script.Parent.MouseEnter:Connect(function()
	script.Parent.Font = Enum.Font.SourceSans
	script.Parent:TweenSize(UDim2.new(0, 265,0, 60), 'InOut', 'Sine', .3, true)
end)

script.Parent.MouseLeave:Connect(function()
	script.Parent.Font = Enum.Font.SourceSansLight
	script.Parent:TweenSize(UDim2.new(0, 265,0, 50), 'InOut', 'Sine', .3, true)
end)

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:WaitForChild('Humanoid')
	
	if player.TeamColor == BrickColor.new('Really black') then
		script.Parent.TextLabel.Text = 'Please select a team to play!'
		wait(2)
		script.Parent.TextLabel.Text = 'Hop on into the gameplay!'
	elseif player.TeamColor == BrickColor.Black() then
		local blackteam = true
		game.ReplicatedStorage.ChangeSL:FireServer(blackteam)
		humanoid.Health = humanoid.Health - humanoid.MaxHealth
		script.Parent.TextLabel.Text = 'Preparing your gameplay..'
		wait(3)
		script.Parent.Parent.Parent.HFrame:TweenPosition(UDim2.new(0,0,0,0), 'InOut', 'Sine', .6)
		wait(1)
		script.Parent.Parent.Visible = false
		wait(1)
		script.Parent.Parent.Parent.HFrame:TweenPosition(UDim2.new(0,0,1,0), 'InOut', 'Sine', .6)
	end
end)
--server script
game.ReplicatedStorage.ChangeSL.OnServerEvent:Connect(function(plr, blackteam)
	if blackteam then
		plr.RespawnLocation = workspace['Tactical Breach Intervention Spawn']
	end
end)

still teleports me to a random position, any idea why?

1 Like

@LukaDev_0 @Xx_FROSTBITExX just realised what you said, and it worked, thank you

2 Likes