Teleporting clothes from one place to another

Hello, so I have these two codes. I have a character costomizer, and im trying to make it so that when you are done costomizing, and you press play, it brings you to another place. Im trying to make i so that when you teleport to the new place, the pants that you had on in the character costomizer is still on your character.

Script in Character costomizer in StarterPack:

local TeleportService = game:GetService("TeleportService")

local player = game.Players.LocalPlayer

local teleportOptions = Instance.new("TeleportOptions")

local teleportData = {

Pants = player.Character:WaitForChild("Pants")

}

teleportOptions:SetTeleportData(teleportData)

TeleportService:TeleportAsync(8657574891, {player}, teleportOptions)

Script in the place you are teleporting to in StarterPack:

local TeleportService = game:GetService("TeleportService")

local teleportData = TeleportService:GetLocalPlayerTeleportData()
if teleportData then
	local Pants = teleportData.Pants
end

Anyone know the problem

You cannot use objects as teleport data, so just send the ID of the clothing item instead.

teleportData = {
      Pants = player.Character:WaitForChild("Pants").PantsTemplate
}

Then in the other place just create some new pants, put them on the player and make the ID of those pants the ID you sent through in the teleportData

so at the end of the script in the place just put

player.Character.Pants.PantsTemplate = Pants

?

1 Like

and also replace the thing you fixed for me

1 Like

That is correct I believe. And your other comment is too.

Okay thanks im going to try it right now.

When i test it says attemp to index nil with “Pants”

actually that not the problem, it says an error for line 8, idk how to fix it

This probably means that the player’s character hasn’t loaded in yet

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

What that line of code does is 1. It gets the player’s Character, but if the player’s is nil, then it will wait for the CharacterAdded event to fire and when it does it will return the players character.

Now you should be able to change the players clothing.

character:WaitForChild(“Pants”)

But since that script is a local script only that one player will be able to see that change. If you want other players to see it then instead of getting the player’s character and changing the clothing on the client, you should send a RemoteEvent to the server with the teleportdata, and then change the clothing from there. (See RemoteEvent | Roblox Creator Documentation for more info)

Its saying something like Teleport async can only be ran the a server script, and i dont know how to put that in a server script

Try copying and pasting your script from a LocalScript to a Script.

This won’t work, since the OP references the player using the LocalPlayer.


All you need is a way to get a specific player. If you’re just testing, you can get the player with something like:

wait(5) local player = Players:GetChildren()[1] -- Gets the first player in the game after 5 seconds

You can also use a proximity prompt inside a part like this:

local TeleportService = game:GetService("TeleportService")

local proximityPrompt -- Set to the proximity prompt, probably something like script.Parent.ProximityPrompt

proximityPrompt.Triggered:Connect(function(player)
    local teleportOptions = Instance.new("TeleportOptions")

    local teleportData = {
    Pants = player.Character:WaitForChild("Pants").PantsTemplate
    }
    teleportOptions:SetTeleportData(teleportData)

    TeleportService:TeleportAsync(8657574891, {player}, teleportOptions)
end)

To handle this in the other server you can use this code from the wiki:

local Players = game:GetService("Players")
 
local function onPlayerAdded(player)
    local joinData = player:GetJoinData()
    local teleportData = joinData.TeleportData
    local pantsContent = teleportData.Pants
 
    print(player.Name.." joined with pants "..pantsContent)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

How would I do it without the proximity prompt, or how do i set up the proximity prompt. Should i just put it in a random part and put the script inside there and locate the script in the part as proximity prompt?

Just fill out the line that has the proximity prompt:

All you need to do is set the proximity prompt to whatever proximity prompt you want.

It doesn’t really matter, besides that you need to get the ProximityPrompt. Normally I’d just put them in the proximity prompt so I can get the prompt with script.Parent.

Note this is just test code showing you how to use the API. I’d recommend just playing around with it.

Okay, and I put both of the scripts inside of ServerScriptService right?

Ok so i didnt really like the idea of the proximity prompt so I hope theres another way. I tried a different method. here it is

‘’'lua

    local TeleportService = game:GetService("TeleportService")
    local Players = game:GetService("Players")
    
    local function onPLayerRemoved(player)
local teleportOptions = Instance.new("TeleportOptions")

local teleportData = {
	Pants = player.Character:WaitForChild("Pants").PantsTemplate
}
teleportOptions:SetTeleportData(teleportData)

TeleportService:TeleportAsync(8657574891, {player}, teleportOptions)

end

Players.PlayerRemoving:Connect(onPLayerRemoved)

‘’’
For some reason it didnt work, but im just trying to avoid the proximity promp method

Again, using a proximity prompt is just for testing. You can run the code when ever you want to teleport the player, when they fire a proximity prompt is just an example.

It probably didn’t work because PlayerRemoving fires when the player leaves. A player that has already requested to leave the game probably can’t be teleported. (Also note that teleporting only works outside of studio.)

So what should I do instead or player removing?

What ever you want: it depends when you want the player to teleport :sweat_smile:

When do you want to teleport the players? You can get the players in a server with game:GetService("Players"):GetPlayers(), which returns a list of players.

Well, i dont know any other methods, is there a way to do it with just getting the player in a server script, then i can go from there, Because localplayer doesnt work in server scripts