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
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)
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.
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.)
What ever you want: it depends when you want the player to teleport
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