Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do a teleport using user input service?

I tried using this code but It ain't working:

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.B then
		local me = game.Players.LocalPlayer.Character

		me.HumanoidRootPart.CFrame = CFrame.new(0, 0, 0)
	end
end)

2 answers

✓ Accepted answer

I had this problem multiple times, and I know multiple ways to solve it. The first way is to Model:MoveTo() which teleports a person to a specific destination. The downside of using this method is that it does not keep the orientation of the player. Example:

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.B then
        local me = game.Players.LocalPlayer.Character

        me:MoveTo(Vector3.new(0,0,0))
    end
end)

The other option and the best one in my opinion is Model:SetPrimaryPartCFrame. It keeps the orientation and position.

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.B then
        local me = game.Players.LocalPlayer.Character

        me:SetPrimaryPartCFrame(CFrame.new(0,0,0))
    end
end)

Also, another thing you should do is wait for your character to load before your event can begin. So just put this at the top of the event:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

1 comment

0

Thanks Comrade!

First of all, you cant change the player's character using LocalScripts. So you would then use remoteEvents.

Let's say you place the RemoteEvent called 'TeleportEvent' in ReplicatedStorage.

That means the code in the local script would be:

local uis = game:GetService("UserInputService")
local remote = game.ReplicatedStorage.TeleportEvent

uis.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.B then
 remote:FireServer()
    end
end)

And put a server script in the ServerScriptService:

local remote = game.ReplicatedStorage.TeleportEvent

remote.OnServerEvent:Connect(function(plr)
plr.CharacterAdded:Connect(function(chr)
        chr.HumanoidRootPart.CFrame = CFrame.new(0, 0, 0)
end)
end)

2 comments

0

Actually, changing the CFrame of a player's HumanoidRootPart on the client replicates to the server.

0

Oh.

This discussion is preserved as it was. New answers, comments, and votes are closed.