Teleport player to selected spawn location doesn't work correctly

hi! I’m trying to make a custom respawn system which teleports player after spawning to their selected spawn location. Its done by a RemoteEvent and Value “SpawnLocation” in PlayersFolder (screenshot)

So far even tho the game prints that the “SpawnLocation” is indeed a correct value when I check it from server and client side it shows that its nil. When player respawns a different script checks for said value and as it is nil it does not teleport the player.

This happens only after player dies as when selecting spawn location for first time in main menu it respawns and teleports player in to selected location.

Whats wrong with my code or what else should I do to get the results I want (After LocationEvent is fired player is teleported to location)

image

Teleport player to set location server script

LocationEvent.OnServerEvent:Connect(function(Player, SelectedLocation)
	print("Selected location == " .. SelectedLocation)
	Player.Character:WaitForChild("Humanoid")
	local ServerLocationData = ServerStorage:WaitForChild("SpawnLocationData")
	local PlayerDatabase = Player:WaitForChild("Database")
	local SpawnLocationData = PlayerDatabase:WaitForChild("SpawnLocation").Value
	SpawnLocationData = SelectedLocation-- Sets the value so server knows when to respawn
	print(SelectedLocation)
	print(SpawnLocationData)
	if SpawnLocationData ~= nil then
		Player.Character:WaitForChild("HumanoidRootPart").CFrame =  ServerLocationData:WaitForChild(SpawnLocationData).CFrame
	elseif SpawnLocationData == "DetainmentSector" and Player.Team.Name == "Class D Personnel" then
		local array = ServerLocationData.ClassDLocations:GetChildren()
		local randomSpawn = math.random(1, #array)
		Player.Character:WaitForChild("HumanoidRootPart").CFrame = array[randomSpawn].CFrame
	else
		NotifyEvent:FireClient(Player, "Deployment Failed", "Deploy location is not nil \n Please report this issue to Manufacturing Department \n Spawning at Default location")
		Player.Character:WaitForChild("HumanoidRootPart").CFrame =  ServerLocationData:WaitForChild("MainFacility").CFrame
	end
end)

Part of respawn player local script

local SpawnLocationData = PlayerDatabase:WaitForChild("SpawnLocation")


LocationEvent:FireServer(SpawnLocationData)
6 Likes

In the localscript you pass the SpawnLocation object instead of the SpawnLocation value.
Then you make the SpawnLocation’s value equal to an object instead of an actual value and since the spawnlocation is a text value not an object taking variable. I think it is causing it to be nil. But that’s just my view.

3 Likes

I did notice it already and fix it. I also moved it so the value in player database is set after teleporting the player.

I don’t know why but now it shows that the value is MTFBase or any other location, just example.
However it still doesn’t respawn player after spawn

LocationEvent.OnServerEvent:Connect(function(Player, SelectedLocation)
	print("Selected location == " .. SelectedLocation)
	Player.Character:WaitForChild("Humanoid")
	local ServerLocationData = ServerStorage:WaitForChild("SpawnLocationData")
	local PlayerDatabase = Player:WaitForChild("Database")
	if SelectedLocation ~= nil then
		Player.Character:WaitForChild("HumanoidRootPart").CFrame =  ServerLocationData:WaitForChild(SelectedLocation).CFrame
		PlayerDatabase:WaitForChild("SpawnLocation").Value = SelectedLocation
	elseif SelectedLocation == "DetainmentSector" and Player.Team.Name == "Class D Personnel" then
		local array = ServerLocationData.ClassDLocations:GetChildren()
		local randomSpawn = math.random(1, #array)
		Player.Character:WaitForChild("HumanoidRootPart").CFrame = array[randomSpawn].CFrame
		PlayerDatabase:WaitForChild("SpawnLocation").Value = SelectedLocation
	else
		NotifyEvent:FireClient(Player, "Deployment Failed", "Deploy location is not nil \n Please report this issue to Manufacturing Department \n Spawning at Default location")
		Player.Character:WaitForChild("HumanoidRootPart").CFrame =  ServerLocationData:WaitForChild("MainFacility").CFrame
	end
end)

Deathscreen & respawn

local plr = game:GetService("Players").LocalPlayer
local LoadCharacterEvent = game:GetService("ReplicatedStorage"):WaitForChild("LoadCharacterEvent")
local LocationEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("LocationEvent")
local PlayerDatabase = plr:WaitForChild("Database")
local Screen = script.Parent.Frame
local FX = script.SFX
local Bar = Screen.Line
local Txt1 = Screen.TextLabel
local Txt2 = Screen.TextLabel2
local Counter = Screen.Counter
local ReasonTxt = Screen.Reason

plr.Character:WaitForChild("Humanoid").Died:connect(function()
	Screen.Visible = true
	FX.BackgroundFX:Play()
	local respawnTime = 15
	for count = 1, 15 do
		wait(0.1)
		Counter.TextTransparency = Counter.TextTransparency - 0.2
		Txt1.TextTransparency = Txt1.TextTransparency - 0.2
		Txt2.TextTransparency = Txt2.TextTransparency - 0.2
		ReasonTxt.TextTransparency = ReasonTxt.TextTransparency - 0.2
	end
	for count = 1,15 do
		wait(1)
		respawnTime -= 1
		Counter.Text = respawnTime
		FX.Tick:Play()
	end
	for count = 1, 5 do
		wait(0.1)
		Counter.TextTransparency = Counter.TextTransparency + 0.2
		Txt1.TextTransparency = Txt1.TextTransparency + 0.2
		Txt2.TextTransparency = Txt2.TextTransparency + 0.2
		ReasonTxt.TextTransparency = ReasonTxt.TextTransparency + 0.2
	end
	LoadCharacterEvent:FireServer()
	Screen.Visible = false
	local SpawnLocationData = PlayerDatabase:WaitForChild("SpawnLocation").Value
	LocationEvent:FireServer(SpawnLocationData)
	FX.BackgroundFX:Stop()
end)

It literally says that the selected locaition exists but it doesnt teleport the player
image

5 Likes

Any error code maybe?

And the elseif statement never will be called since, if the selectedlocation is not nil then always the first will execute and the second will never.

4 Likes

Oh yeah, thanks you just solved another issue which I had which was why respawn for Class Ds wasn’t working. And no I do not encounter any errors as the value is not nil.
I think the game first trys to teleport the player mayby and then respawns them. Even tho it is illogical as the script does that in different order I will make the location event wait for teleporting the player until the player has 100 hp.

4 Likes

Try that. If you will still have the issue then please don’t be afraid to tell it.

4 Likes

Adding a

repeat
		wait(0.5)
	until Player.Character:WaitForChild("Humanoid").Health == 100

didn’t work. It still prints the location but does not teleport the player

4 Likes

I would recommend doing print/warn debugging method in order to get a bit more detail where the code stops working…

4 Likes

I did that. According to the code it does teleport player but it does not.

image

LocationEvent.OnServerEvent:Connect(function(Player, SelectedLocation)
	repeat
		wait(0.5)
	until Player.Character:WaitForChild("Humanoid").Health == 100
	print("Selected location == " .. SelectedLocation)
	local ServerLocationData = ServerStorage:WaitForChild("SpawnLocationData")
	local PlayerDatabase = Player:WaitForChild("Database")
	if SelectedLocation == "DetainmentSector" and Player.Team.Name == "Class D Personnel" then
		local array = ServerLocationData.ClassDLocations:GetChildren()
		local randomSpawn = math.random(1, #array)
		Player.Character:WaitForChild("HumanoidRootPart").CFrame = array[randomSpawn].CFrame
		PlayerDatabase:WaitForChild("SpawnLocation").Value = SelectedLocation
		print("Teleporting player to "..SelectedLocation)
	elseif SelectedLocation ~= nil then
		Player.Character:WaitForChild("HumanoidRootPart").CFrame =  ServerLocationData:WaitForChild(SelectedLocation).CFrame
		PlayerDatabase:WaitForChild("SpawnLocation").Value = SelectedLocation
		print("Teleporting player to "..SelectedLocation)
	else
		NotifyEvent:FireClient(Player, "Deployment Failed", "Deploy location is not nil \n Please report this issue to Manufacturing Department \n Spawning at Default location")
		Player.Character:WaitForChild("HumanoidRootPart").CFrame =  ServerLocationData:WaitForChild("MainFacility").CFrame
	end
end)```
4 Likes

maybe trying to print out the user’s position after changing it to the new position?

4 Likes

I added

wait(5)

before teleporting player to the location. And it works with such a huge delay.
So it seems like it just tried to teleport dead player (?).

4 Likes

Since the character loading depends on the Client’s latency with the server it may cause issues if you run the code too early. Which means if you can use the propertychanged event to see when the health has been changed and then check if it changed to 100 then trying to teleport would fix the issue maybe

4 Likes

And also change the status of this post to solved.

3 Likes

I think I will try merging 2 scripts, Selected location and Load Character. It will most likely remove my issues

3 Likes

I have solved the issue by merging LoadCharacter and SelectLocation events in to one and then adding a wait for the character to load

LocationEvent.OnServerEvent:Connect(function(Player, SelectedLocation)
	Player:LoadCharacter()
	repeat wait()
		until Player.Character:WaitForChild("HumanoidRootPart")
	print("Selected location == " .. SelectedLocation)
	local ServerLocationData = ServerStorage:WaitForChild("SpawnLocationData")
	local PlayerDatabase = Player:WaitForChild("Database")
	if SelectedLocation == "DetainmentSector" and Player.Team.Name == "Class D Personnel" then
		local array = ServerLocationData.ClassDLocations:GetChildren()
		local randomSpawn = math.random(1, #array)
		Player.Character:WaitForChild("HumanoidRootPart").CFrame = array[randomSpawn].CFrame
		PlayerDatabase:WaitForChild("SpawnLocation").Value = SelectedLocation
		print("Teleporting player to "..SelectedLocation)
	elseif SelectedLocation ~= nil then
		Player.Character:WaitForChild("HumanoidRootPart").CFrame =  ServerLocationData:WaitForChild(SelectedLocation).CFrame
		PlayerDatabase:WaitForChild("SpawnLocation").Value = SelectedLocation
		print("Teleporting player to "..SelectedLocation)
	else
		NotifyEvent:FireClient(Player, "Deployment Failed", "Deploy location is not nil \n Please report this issue to Manufacturing Department \n Spawning at Default location")
		Player.Character:WaitForChild("HumanoidRootPart").CFrame =  ServerLocationData:WaitForChild("MainFacility").CFrame
	end
end)
3 Likes

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