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)
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)
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.
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.
I did that. According to the code it does teleport player but it does not.
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)```
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
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)