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

Why isn't my teleport script teleporting me?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
Yea that would work but the problem is that my character won't teleport. Green_Lime2 80 — 4y
0
Yes, I know that. I'm telling you that if you say Torso.Position instead of rootpart.Position, it might fix it. RhysRocksRules 38 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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.

Answer this question