Why is it not teleporting the player?

I want it so if the player dies in the current map, they should respawn in the map.

The issue is that the CharacterAdded event runs and works fine, its just not teleporting the player to the map.

I’ve searched this same issue up, no results.


game.Players.PlayerAdded:Connect(function(player)
	
	print(player, " ADDED!")
	
	player.CharacterAdded:Connect(function(character)
		print("CHARACTER", " ADDED!")
		
		if GIP.Value == true then
			print("yea fo sho") -- this prints 
			spawnPlayer(character, CurrentMap) -- this function does work, it teleports the players at a random spawn. It just doesn't work when the CharacterAdded event runs.
		elseif GIP.Value == false then
			print("nah")
			spawnPlayer(character, workspace.Lobby) -- this function does work, it teleports the players at a random spawn. It just doesn't work when the CharacterAdded event runs.
		end
		
	end)
	
end)

1 Like

Can you show us the code inside the spawnPlayer function

1 Like

pretty sure characteradded event is only fired when the player first instantiates their character into the game. instead you could have a game.Workspace.ChildAdded then check if the child (first parameter) is a character, then tp them from there

1 Like

for example

game.Workspace.ChildAdded:Connect(function(child)
   local player = game.Players:FindFirstChild(child.Name)
   if (player) then
      -- tp
   end
end)
1 Like

same issue.

workspace.ChildAdded:Connect(function(child)
	print("added") -- this prints
	local player = game.Players:FindFirstChild(child.Name)
	print("found player") -- this prints
	if (player) then
		if GIP.Value == true then
			print("game is in progress") -- this prints
			spawnPlayer(child, CurrentMap) -- this doesnt work,but works any other time
		elseif GIP.Value == false then
			print("not in progress") -- this prints
			spawnPlayer(child, workspace.Lobby) -- this doesnt work,but works any other time
		end
	end

end)

here is the spawnPlayer() function:

local function spawnPlayer(character, map)
	--
	local spawns = map:WaitForChild("Spawns"):GetChildren()
	local randomSpawn = spawns[math.random(1, #spawns)]
	
	character:WaitForChild("Humanoid").Health = 100
	character:WaitForChild("Humanoid").MaxHealth = 100
	
	if character then
		character:SetPrimaryPartCFrame(randomSpawn.CFrame)
	end
end

.CharacterAdded is fired each time the player’s character is loaded. This occurs, for example, each time you reset upon your old character cleaning up and your new one loading.

before setting primarypartcframe add a wait()

well you learn something new everyday lol :upside_down_face:

1 Like

I figured it out, the player spawns to fast

local function spawnPlayer(character, map)
	--
	local hrp = character:WaitForChild("HumanoidRootPart")
	local spawns = map:WaitForChild("Spawns"):GetChildren()
	local randomSpawn = spawns[math.random(1, #spawns)]

	character:WaitForChild("Humanoid").Health = 100
	character:WaitForChild("Humanoid").MaxHealth = 100
	character:SetPrimaryPartCFrame(randomSpawn.CFrame)
end

The PrimaryPart of the player’s character model is the "HumanoidRootPart
all you needed do was wait for that to load before attempting to move the model itself.

1 Like