Teleporting player in local script

More than two weeks ago, I tried to make a script whose task was to teleport the player who was activating ProximityPromt.

The task seemed simple as I usually do. Unfortunately, nothing worked in this case. There was also no error on the console.

Explorer photo

Local script
script.Parent.ProximityPrompt.Triggered:Connect(function()
	local Players = game:GetService("Players") 
	local Player = Players.LocalPlayer
	local Character = Player.Character or Player.CharacterAdded:Wait()
	Character:MoveTo(game.Workspace.LeaveHouseTeleport.Position)
end)

I tried to fix everything from moving the script to server script and ending with a complete script change. Neither of these actions worked or generated further errors.

I also asked people on the dev forum, but they didn’t tell me anything specific, just asked to send the entire script, or they wrote the script from scratch, completely out of context, e.g. adding debounce, changing ProximityPromt to a UI button or something like that.

I’ve already done a similar script in the GUI and it worked without a problem. Now, however, difficulties have arisen.

maybe this? I am still pondering my coding skills but this may help.

local Teleport = "part"
function Touch(hit)
	if script.Parent.Locked == false and script.Parent.Parent:findFirstChild(Teleport).Locked == false then script.Parent.Locked = true script.Parent.Parent:findFirstChild(Teleport).Locked = true
	local Pos = script.Parent.Parent:findFirstChild(Teleport)
		hit.Parent:moveTo(Pos.Position) wait(1) script.Parent.Locked = false script.Parent.Parent:findFirstChild(Teleport).Locked = false end end 
script.Parent.Touched:connect(Touch)

The problem is that you have your LocalScript in workspace wich you shouldn’t because LocalScripts are client side

to fix that replace it with a Script

The Script

local prompt = script.Parent.ProximityPrompt

prompt.Triggered:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    character:MoveTo(workspace.LeaveHouseTeleport.Position)
end)

I do not think you can use ProximityPrompt with a local script its better to use a normal script.

I changed it to server script but the error “12:49:27.792 Workspace.Room.Door.Union.Script:4: attempt to index nil with ‘Character’” popped out

Try this

local Prompt = script.Parent.ProximityPrompt

Prompt.Triggered:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    character.HumanoidRootPart.CFrame = CFrame.new(workspace.LeaveHouseTeleport.Position)
end)

Prompts work in local scripts, you just have to make sure that whomever triggers it matches the player of which the local script is executing for.

2 Likes
local Script = script
local Workspace = workspace
local Part = Script.Parent
local Prompt = Part.ProximityPrompt
local Teleport = Workspace.LeaveHouseTeleport

local function OnPromptTriggered(Player)
	local Character = Player.Character
	if not Character then return end
	Character:PivotTo(Teleport.CFrame)
end

Prompt.Triggered:Connect(OnPromptTriggered)
2 Likes