jeb_andDinnerbone
jeb_andDinnerbone

Reputation: 11

How do I teleport a player to a Part when a ProximityPrompt is triggered?

So, I have been trying to make a game with multiple enemies. Now, those enemies, they are caged in a cage far from the Baseplate. I made a ProximityPrompt inside a part which is transparent and have CanCollide off which if triggered will teleport you to the position of another transparent part which is inside the cage. I have been researching on how to do this. I put this code on a LocalScript in the ProximityPrompt:

local teleportPart = game.Workspace.TELEPORT.Position

script.Parent.Triggered:Connect(function()
    game.Players.LocalPlayer.Character:MoveTo(teleportPart)
end)

-- TELEPORT is the transparent part in the cage.

Then, I tested it. However, when the ProximityPrompt is triggered, nothing happens. No errors in the command bar, nothing.

I tried multiple ways such as:

  1. Change game.Players.LocalPlayer.Character:MoveTo(teleportPart) to Character.HumanoidRootPart.CFrame = CFrame.new(2350.5, -6.2, 639.015)
  2. Making the teleportPart said immediately, (sorry I couldn't think of a word for it) example: game.Players.LocalPlayer.Character:MoveTo(game.Workspace.TELEPORT.Postion)
  3. Placing the "Postion" in "MoveTo" and deleting it from the variable. (game.Players.LocalPlayer.Character:MoveTo(teleportPart.Postion))

None have worked, none have errors. What should I do? Thanks.

Upvotes: 1

Views: 531

Answers (1)

Kylaaa
Kylaaa

Reputation: 7188

Your code and all of your previous attempts look fine, but your issue is that LocalScripts only execute from a handful of locations. According to the documentation :

A LocalScript will only run Lua code if it is a descendant of one of the following objects:

  • A Player's Backpack, such as a child of a Tool,
  • A Player's character model,
  • A Player's PlayerGui,
  • A Player's PlayerScripts,
  • The ReplicatedFirst service.

Since your LocalScript is the child of a ProximityPrompt in the Workspace, it simply never executes.

So to fix your issue, convert your LocalScript to a Script and try this. You can get the Player who activated the prompt as an argument from the Triggered event :

local proximityPrompt = script.Parent
local teleportPart = game.Workspace.TELEPORT

proximityPrompt.Triggered:Connect(function(player)
    local character = player.Character
    if character then
        character:MoveTo(teleportPart.Position)
    end
end)

Upvotes: 2

Related Questions