Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I teleport players within a game by touching something with a key?

Asked by 4 years ago

I have made a keycard-like entrance that requires the player to hold and item and touch a secret object, but instead of opening some kind of door it teleports them to a place within the same game, so it's not putting them in a different game. Here is my code:

script.Parent.Bottle.Touched:Connect(function(hit)
    print("touched")
    if hit.Parent.Name == "Key" then
        print("teleport")
        game.Workspace.Player.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(96, 2.5, 48.5))
    end
end)

PS, the prints are for me debugging my code. The error I get is that Player is not a member of Workspace.

1 answer

Log in to vote
0
Answered by
Lakodex 711 Moderation Voter
4 years ago
Edited 4 years ago

Create a part in workspace named "Teleport" (Case sensitive) and use this. The key must be out and the player has to touch the object with the key out. BTW when tools are out they are parented to the character.

script.Parent.Bottle.Touched:Connect(function(hit)
    if hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Key") and hit.Parent:FindFirstChild("Key"):IsA("Tool") then
        print("teleported")
        hit.Parent.HumanoidRootPart.CFrame = game.Workspace.Teleport.CFrame * CFrame.new(0, 5, 0)
    end
end)
0
Thanks! Also thanks for the extra info with the tools being parented to the character, that will definitely help in the future. DiamondMiner199 30 — 4y
Ad

Answer this question