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

Why isn't my teleport script teleporting me?

Hello there, I am making code that will teleport a player to a certain coordinate in the map, but it's not working and the textbutton is saying "Please type a correct value." and I'm not sure what to do now. And by the way, the problem is that my character will not teleport.

Here is the code: (Changed)

local player = game.Players.LocalPlayer
local playergui = player.PlayerGui
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local XCoord = playergui.ScreenGui.X
local YCoord = playergui.ScreenGui.Y
local ZCoord = playergui.ScreenGui.Z
local Teleport = script.Parent

Teleport.MouseButton1Click:Connect(function()
	if typeof(tonumber(XCoord.Text)) or typeof(tonumber(YCoord.Text)) or typeof(tonumber(ZCoord.Text)) ~= "number" then
		script.Parent.Text = "Please type a correct value."
		wait(2)
		script.Parent.Text = ""
	else
		character:MoveTo(Vector3.new(XCoord.Text, YCoord.Text, ZCoord.Text))
		
	end
end)

Any help would be appreciated!

3 answers

Instead of teleporting the HumanoidRootPart, try teleporting the Torso of the player. Like this:

local player = game.Players.LocalPlayer;
local playergui = player.PlayerGui;
local character = player.Character;
local humanoid = character:WaitForChild("Humanoid");
local textbox = playergui.ScreenGui.TextBox;
local Teleport = script.Parent;
	 
Teleport.MouseButton1Click:Connect(function()  
	if tonumber(textbox.Text) ~= nil and typeof(textbox.Text) == "number" then
		player.Torso.Position = Vector3.new(textbox.Text); --Line to pay attention to
	else
		textbox.Text = "Please type a correct value.";
	end
end)

If this helps, please mark it, if you're having trouble, please comment and let me know, I will try to help.

2 comments

0

Yea that would work but the problem is that my character won't teleport.

0

Yes, I know that. I'm telling you that if you say Torso.Position instead of rootpart.Position, it might fix it.

idk about this but i think the problem is that is wont teleport becuz textbox.text isnt a proper coordinate idk what exactly what textbox.text is but if its not like for example 1,1,1 or 123,76,87 which are all virtually valid inputs for a position then if probably not gonna work unless u say for example game.workspace.part.position which is the position of a named part unless that its likely not gonna work which could mean why its asking u to "Please type a correct value." cuz its probably not a valid value

Line 9, the condition typeof(textbox.Text) == "number" will always become false because the value textbox.Text will always become a string, thus I suggest rewriting the condition so that each separated value (by a comma) is a number.

Sample: a function which checks whether the string given can "fit" Vector3.new.

local function potato(a)
	for _,b in next,string.split(a,',')do
		if not tonumber(b)then return false;end
	end
	return true
end

print(
	potato('1,1,1'),-- true
	potato('1,wowpotato,1'),-- false
	potato('1,1'),--true
	potato('1')--true
)

Notes: You can't teleport by setting with "Position", use "CFrame" instead. You can't set Vector3.new arguments with strings, turn them into numbers first.

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