Respawn Script not Working

To keep it simple, I am making a script that makes it so the player respawns to a specific part when they die after they trigger a proximity prompt.

The script should work fine, and it prints out the statements I have.

My problem comes at the teleportation logic. For some reason, it doesn’t teleport the player to the specified part.

This is strange, because I used the same teleportation logic in a different script and it works.

Here is my script (it is inside the part that has the proximity prompt):

local RespawnPart = game.Workspace.StageSpawnPoints.Cloud.Stage1Spawn 
local SpawnSetPart = game.Workspace.Stages.Cloud.Stage1.Portal.SpawnSetPart

local proxtrig = false

local Players = game:GetService("Players")

SpawnSetPart.ProximityPrompt.Triggered:Connect(function()
	proxtrig = true
end)


local function onPlayerAdded(player)
	local function onCharacterAdded(character)
		local humanoid = character:WaitForChild("Humanoid")

		humanoid.Died:Connect(function()
			if proxtrig == true then
				print("true")
				
				wait(5)
			
				local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
				
				if humanoidRootPart then
					humanoidRootPart.CFrame = CFrame.new(RespawnPart.Position)
					print("should work")
				end

				else
				
				print("proxtrig false")
			end
		end)
	end

	player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

When the player dies, the character gets destroyed and a new one is created. You should take this into consideration and after the player has respawned, set the character to the newly spawned one. So,
before the local humanoidRootPart = character and ..., you should put this line:
character = player.Character

Also, you could do this differently and completely remove the humanoid.Died connection and teleport the player directly in the onCharacterAdded function. Just move this block of code

into the earlier mentioned function. Be careful, though, since this function will trigger everytime the player’s character is deleted and new one is created. This then isn’t limited only to the player’s death.

1 Like

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