Player doesnt teleport after they there death

I’m currently creating the teleport script.
I am making the script that teleports the player after they died.
But the error don’t appears and I can not find any bugs from them.
Player dies but doesnt teleport.
There is already player local at upper.

button1.MouseButton1Down:Connect(function()
	teleportframe.Visible = false
	local humanoidrootpart = player.Character:FindFirstChild("HumanoidRootPart")
	
	if humanoidrootpart then
		local char = player.Character
		local h = char:WaitForChild("Humanoid")
		h.Health = 0
		wait(8)
		humanoidrootpart.CFrame = CFrame.new(part1.CFrame.p) + Vector3.new(0,5,0)
	end
end)
2 Likes

Are you waiting on the character respawn?

Yes. I am making this teleport system for my train game, and when they are on the train they are going to welded to train so I have to kill the player before they get teleport. if I didnt train will be teleported too.

1 Like

The problem is that you’re not changing the reference for humanoid root part. Also, killing them is rather inefficient, should just reload their character. The below code should work:

button1.MouseButton1Down:Connect(function()
	teleportframe.Visible = false
	local humanoidrootpart = player.Character:FindFirstChild("HumanoidRootPart")
	
	if humanoidrootpart then
		player:LoadCharacter()
		local char = player.Character or player.CharacterAdded:Wait()
		humanoidrootpart = char:WaitForChild("HumanoidRootPart")
		humanoidrootpart.CFrame = CFrame.new(part1.CFrame.p) + Vector3.new(0,5,0)
	end
end)
2 Likes

If the problem is solved, please mark it as such to avoid further replies.

I tried out with the new script to the old script, but it seems like there is a bug. I didn’t see a error but when I pressed the button the ui visibility will be turned off but the player didn’t die and didn’t get teleported.

All deaths &/or player reloads will clear the UI or reset them. Would be best to set them to not reset on spawn. CFrame.new(part1.CFrame.Position) + CFrame.new(0, 5, 0) would also be the best way to define the teleport line.

2 Likes

If not respawn and train with welded the train will be teleported also. So I wanted to make like kill then teleport.

2 Likes

Or you could just find all welds tying the character to the train (if all are on character, do:

for _,v in pairs(char) do
 if v:IsA('Weld') then
  v:Destroy()
 end
end
3 Likes

If you want the best method to do it, make an iterating loop that finds all welds that attach the character to the train and destroy them before teleporting the player. No need for respawn.

I will try it. Thanks. It might take some time.


How should I deal with this.

LoadCharacter has to be called by the server. I’d suggest adding a remote event that gets fired to the server and the server just has code like:

(remote event).OnClientEvent:Connect(function(plr)
	plr:LoadCharacter()
	local char = plr.Character or player.CharacterAdded:Wait()
	humanoidrootpart = char:WaitForChild("HumanoidRootPart")
	humanoidrootpart.CFrame = CFrame.new(part1.CFrame.p) + Vector3.new(0,5,0)
end)

Be sure to define part1 on the server side as the part you want, or fire it in the event as an argument

Remote events do not require a player to be passed as a value if firing from the client, as the first argument is always the player who fired the server event.

Hope this helps!

1 Like