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

This script gives me an error and won't let me teleport?

So I put this script in a normal script but for some reason it gives this error:

Unable to cast string to int64 Stack Begin Script 'Workspace.Tp1.tppart.Script', Line 6 - function onTouched Stack End

Here is the script:

function onTouched(hit)
	character = hit.Parent
        if character then
	wait(1)
	print("Teleporting...")
            game:GetService("TeleportService"):Teleport(game.StarterGui.OpenCloseGUI.Frame.TextBox.Text, character)
print("Success?")
print("Value = "..script.Parent.Value.Value)
            end
        end
 
script.Parent.Touched:connect(onTouched)

2 answers

Teleport takes two arguments: the player, then the place Id you want to be teleported to. (Looks something like 200445)

It only works in game, not studio so test in game once you fix the error.

#edit: code

changed a bit about your code.

you were getting the gui from the startergui which is not the player's gui. it The player gets a copy of that gui in startergui and then it gets put into the player's playergui.

also if the user enters a placeid in the text it needs to be a number.

there's also the scenario where the text entered isn't a placeId. we have pcall to stop script from erroring if that's the case. pcall takes code inside of a function and stops the code from breaking if the code inside pcall errors.

local Players = game:GetService("Players")

function onTouched(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	local placeId = tonumber(player.PlayerGui.OpenCloseGUI.Frame.TextBox.Text)

	if Player then
		wait(1)
		print("Teleporting...")

		--we're wrapping the function inside of a pcall 
		--this is because if you're figuring out where to teleport based off a gui's text, then the player 
		--might enter in a wrong placeid that doesnt load
		local success, err = pcall(function()
			game:GetService("TeleportService"):Teleport(Player, placeId)
		end)

		--do something based on if the teleport was successful 
		if success then
			print("succesfully teleported player")
		else
			error("[WE HAD AN ERROR WHILE TELEPORING]: ", err) 
		end

		print("Value = "..script.Parent.Value.Value)
	end
end

script.Parent.Touched:Connect(onTouched)

1 comment

0

pls respond

The first thing you are doing wrong is getting the Character when what you actually need is the Player if you go to the wiki page for Teleport you'll see it takes in the Player as an argument and not the Character,

The other thing is that you are getting the ID from StarterGui when what you actually want is to get it from Player.PlayerGui as that is where the Players copy of the GUI is located, you also didn't tonumber the string from the TextBox

This should work:

local Players = game:GetService("Players")
function onTouched(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent) -- Getting the Player from it's Character
	if Player then -- Check if hit.Parent is a Player's Character
		wait(1)
		print("Teleporting...")
game:GetService("TeleportService"):Teleport(tonumber(Player.PlayerGui.OpenCloseGUI.Frame.TextBox.Text), Player)
		print("Success?")
		print("Value = "..script.Parent.Value.Value)
	end
end

script.Parent.Touched:connect(onTouched)

Wiki Pages:

Teleport

PlayerGui

GetPlayerFromCharacter

tonumber

Don't forget to mark my answer as the solution if it solved your problem :)

7 comments

0

Im am gonna try it now

0

I got this error: 10:54:28.536 - Argument 1 missing or nil

0

Which line?

0

Do you have discord? That would make it much easier and quicker to communicate

0

yeah its PersonalSlash#9004

0

Its Line 7 btw

0

It might be because you are trying to get the Text in a server script

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