Teleportation Trouble

Hello. I made a script where if a player steps on the parent part, it teleports the player to any position I set it to. I have a model with 2 bricks in it to give me an idea of how big the player is compared to the end location so I don’t get the player stuck. I send the player to one of the brick’s position.

It worked fine until now. For some reason after placing another teleporter into the game, it will only send the player to a different location than what I tell the game. I’m sorry if my explanation is poor.

Here is my script:

    function onTouched(hit)  
  print("Teleport")
  local human = hit.Parent:findFirstChild("Torso")
  local brick = script.Parent
  if (human ~=nil) then
    human.CFrame= CFrame.new(Vector3.new(638.972, 214.33, 342.934))
   end
end

script.Parent.Touched:connect(onTouched)

Edit: I found the problem. The script works fine (I turned on R6 only). Allow me to explain.

So in the game, the teleporter is an invisible brick that is inside a hole in the wall. It’s supposed to teleport you to a different hole in the wall. I was too lazy to manually create another wall, so I copied and pasted the current one. I unknowingly duplicated the teleporter. I duplicated it before I set the coordinates to the location I wanted it so it instead sent me to the coordinates that were already in the script (I copied the script so I wouldn’t have to type it all over again). So the script worked fine, I just didn’t know this stupid mistake.

Long story short, the script works fine, I just made a little mistake with my building and made you guys go through all the stress of trying to help me with a battle that was impossible to win. Thank you guys anyway for the help, I hope I make no more mistakes like this.

1 Like

If I’m understanding correctly, this should work:

script.Parent.Touched:Connect(function(hit)
	print("Teleport")
	local HumanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")
	if HumanoidRootPart and game.Players:GetPlayerFromCharacter(hit.Parent) then
		HumanoidRootPart.CFrame = CFrame.new(638.972, 214.33, 342.934)
	end
end)

If this doesn’t work, do you mend mentioning if there are any errors or anything that you notice isn’t working correctly?

EDIT: Added a line that checks if it is a player that gets teleported based off of what @6Clu said.

2 Likes

If you try to teleport using the Torso you can get unexpected results - use the HumanoidRootPart instead as this is the root part of the character.

You may also want to do Player.Character:SetPrimaryPartCFrame() instead of setting the CFrame of the part manually.

Please be aware as well, your teleport doesn’t check if they actually are a player or not (just if a brick called Torso exists).

1 Like

I think it might be my studio. It kept acting really strange around when the issues formed and now it won’t even save for me. It also won’t load any assets even though I’m online. I’ll just try again another time.

1 Like

I think Roblox is having some issues right now because I’m getting HTTP 503 errors. You should still implement the changes discussed by myself and @6Clu because they are good practices for this kind of thing. :slight_smile:

1 Like

local human = hit.Parent:findFirstChild(“Torso”)

Means you aren’t teleporting to player, you are teleporting to “Torso” set “Torso” to “Player”

1 Like

If you didn’t get a notif for my edit, I made a building mistake. I hope you got notified of this comment so I can tell you to read the edit. Thank you all for your help, you’re all awesome.

2 Likes