Setting specific spawn locations

i wanna make it so when the player joins from a place they get a specific spawn location, the only problem i have is making the spawn location different, otherwise the script works.

which the part i need help with is this:

players.LocalPlayer.RespawnLocation = game.Workspace.SpawnLocation

this is the entire script for reference

local players = game:GetService("Players")

local place1Id = 128194721848749
local place2Id = 136403810915632
local place3Id = 133711048400377


	local joinData = game:GetService("TeleportService"):GetLocalPlayerTeleportData()

if not joinData then return end


	if joinData.comingFromPlace == place1Id then
		print("spawn 1")

players.LocalPlayer.RespawnLocation = game.Workspace.SpawnLocation
		
	end
	if joinData.comingFromPlace == place3Id then
		print("spawn 2")
	players.LocalPlayer.RespawnLocation = game.Workspace.SpawnLocation2
	end

Is there any error coming from this or is it just not spawning the player in that spot?


Based on the Roblox Creator Hub Documentation site page for Player.RespawnLocation, the SpawnLocation must meet the following requirements in order to have the player respawn there:

The first condition was met, so you may just need to check what its TeamColor property is set to if it’s also meant to be specific to a team, or, if it’s not, make sure its Neutral property is set to true.


If both of those conditions aren’t met, then it won’t spawn the player on that RespawnLocation:

If RespawnLocation is not set to a valid SpawnLocation then the default spawning logic will apply. For more information on this see the page for SpawnLocation.

just not spawning the player in the spot

the print(“spawn1”), spawn 2 works

You need to use Player:LoadCharacter() to properly spawn them on the target spawn point (Do this after setting their respawn point)

Ah, after doing some quick tests in Roblox Studio, it turns out that Player.RespawnLocation only works when it’s set by the server. There are other ways of retrieving JoinData, including through Player:GetJoinData(), so it’d be possible to slightly modify what you had in the original post to handle this from the server-side.

Although I did not test this in a live game when being teleported from another place, I simulated a similar setup by having the script randomly choose a value that is only associated with one spawnpoint and it successfully teleported the player to that spot right away upon join, as well as every time their Character respawned.

The example I created for testing purposes

local Players = game:GetService("Players")

local places = {
	
	[1] = workspace:WaitForChild("SpawnLocation1"),
	[2] = workspace:WaitForChild("SpawnLocation2"),
	[3] = workspace:WaitForChild("SpawnLocation3")
	
}

Players.PlayerAdded:Connect(function(player)
	local randomNumber = math.random(1,3)
	player.RespawnLocation = places[randomNumber]
	
	print(player.RespawnLocation)
end)



Here’s an example of how you could modify the code in the original post to be compatible with a Server Script:

Completed Example Revision (Server Script):

-- Server Script code
local Players = game:GetService("Players")

local place1Id = 128194721848749
local place2Id = 136403810915632
local place3Id = 133711048400377

local places = {
	-- Update these to the intended SpawnLocations for each place
	[place1Id] = workspace:WaitForChild("SpawnLocation1"),
	[place2Id] = workspace:WaitForChild("SpawnLocation2"),
	[place3Id] = workspace:WaitForChild("SpawnLocation3")
}

Players.PlayerAdded:Connect(function(player)
	local joinData = player:GetJoinData()
	local comingFromPlace = joinData.comingFromPlace
	
	if comingFromPlace == nil then return end
	
	player.RespawnLocation = places[joinData.comingFromPlace]
	print(player.RespawnLocation)
end)

do the spawn locations have any properties that i need to turn off?

nvm i fixed it, i am overjoyed

Please mark @StrongBigeMan9 as the solution, thats just greedy to mark yourself.

What ended up fixing it? If it wasn’t any of the things I explained, please explain what the solution was so that anyone else who encounters the same issue and reads through this thread in the future will know what solved it for your use case.

oh sorry i didnt know, ill mark him

instead of putting everything in a single script i used events to trigger the respawns, the script was a regular script with the run context as client

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.