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

Help With Friend Teleport?

local TeleportService = game:GetService("TeleportService")
local Text = script.Parent.FriendName.Text
local Player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
	Text = script.Parent.FriendName.Text
	local FoundPlayer = game.Players:GetUserIdFromNameAsync(Text)
	if FoundPlayer then
		print(FoundPlayer)
 
		local function followPlayer(Player, FoundPlayer) 
			local success, errorMsg, placeId, instanceId = TeleportService:GetPlayerPlaceInstanceAsync(FoundPlayer)
			if success then
				TeleportService:TeleportToPlaceInstance(placeId, instanceId, Player)
			    else
			        print("Teleport error:", errorMsg)
			    end
			end 
		followPlayer(Player, FoundPlayer)
	end
end)

I am trying to allow the player to teleport to another player once a players name is entered and a gui button is clicked but i just get the error

TeleportService::GetPlayerPlaceInstanceAsync can only be called from the server.

1 comment

0

i wanted to learn this too..

1 answer

Okay, first of all, you got a LOT of things wrong here.

  • Use the PlayerGui when changing Text.

-TeleportService is only for teleporting to places, not players. The only way to do this is to use CFrame, as I will show you.

  • LocalPlayer only works in Local Scripts, which your script doesn't appear to represent. But, I will assume that it IS a Local Script.

  • Local variables don't work anymore. They were deprecated after Roblox upgraded from Lua 5.0 to Lua 5.1, and they can now only be used within the same block of code.

Here's the script:

Text = script.Parent.FriendName.Text
Player = game.Players.LocalPlayer
Button = script.Parent
function ChangeText(text)
	for _, g in pairs(game.Players:GetPlayers()) do
		g.PlayerGui.ScreenGui.FriendName.Text = text
		if type(text) == "number" then
			g.PlayerGui.ScreenGui.FriendName.Text = tostring(text)
		end
	end
end
Button.MouseButton1Down:Connect(function()
	ChangeText(Text) -- Is this what you're trying to do?
	local FoundPlayer = FoundPlayer -- Idiom statement defining a local as a global
	FoundPlayer = game.Players:GetUserIDFromNameASync(Text)
	if FoundPlayer then
		print(FoundPlayer)
		local function followPlayer(Player, foundPlayer)
			local success = Player.Torso.CFrame:toWorldSpace(foundPlayer)
			if success then
				Player.Torso.CFrame = foundPlayer.Torso.CFrame
			else then error("Error")
		end
	end
end
	

1 comment

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