G’day all yous, I’m making a game that has its start “hub” game where you can pick between the campaign missions (which are all separate games and i am trying to find an alternative) or multiplayer.
What do you want to achieve?
What i am trying to achieve here is two things that are similarly related: A tool chooser where you can pick from a list of Primary weapons (rifles and such) and secondaries (pistols and such), and a way for the separate campaign mission games to know what guns the player chose, and for them to be cloned to the players backpack when the time is right (e.g when the beginning cutscene finishes and the player can start to control the character)
What is the issue? So far i have had no clue on how to make the gui for the player to choose the weapons, and the way for the chosen guns to “travel through” the main game and for the player to have them in the separate weapons
What solutions have you tried so far? Usually on Dev Hub, there will almost always be someone who has the same problem, but after searching high and low, i have realised my problem is so niche that somehow there is no dev hub forum to help me. So i used the AI assistant on Roblox studio but the fact that it will almost never work and the fact that it always makes a hard coded GUI, even when i have told it that i have already made the GUI
Other than that, If you could at least point me in the right direction i would be forever thankful. All i really need to know is how to just do the persisting weapon choice that travels from one game to another, and i can probably figure the rest out (like gui) myself, and you will DEFINITELY be seeing more questions like this soon from me.
I would look at this link: SetTeleportSetting-RobloxDocs and this link: GetTeleportSetting-RobloxDocs. It looks like you can only use this in a local script but that shouldn’t be too difficult since it seems like you are mainly working with UI in the first place anyways. I think how the TeleportService works is it sends the data across the servers and you use the :GetTeleportSetting method to retrieve the data. Since this data is stored on the client, you should NOT use this for storing important data (hackers can abuse this). Luckily, it looks like all you want to store is weapon type. As long as you include checks on the new server that the user actually owns the weapon you should be fine. I added code examples below.
Client - Server 1 (Teleport script)
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local selectedWeapon = "" -- Put the name of your weapon here before teleport
TeleportService:SetTeleportSetting("SelectedWeapon", selectedWeapon)
local teleportOptions = Instance.new("TeleportOptions")
local toPlaceId = "" -- The ID for your mission place
local jobId = "" -- The ID for the server you are joining
local teleportData = {
placeId = placeId,
jobId = jobId
}
teleportOptions:SetTeleportData(teleportData)
TeleportService:TeleportAsync(placeId, {player}, teleportOptions)
Client - Server 2 (Receive teleport data script; could be put in the same script as the other one)
local TeleportService = game:GetService("TeleportService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage -- Insert remote event location here
local selectedWeapon = TeleportService:GetTeleportSetting("SelectedWeapon")
remote:FireServer(selectedWeapon) -- WARNING: selectedWeapon can be nil if something goes wrong
Server - Server 2 (Check that the player actually owns the weapon they selected)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage -- Insert remote event location here
remote.OnServerEvent:Connect(function(player: Player, selectedWeaponName: string)
-- Check if the player owns the weapon
end)
Edit: You probably should actually be following the above and the :Get/SetTeleportData method does not store custom data.
You can also use :GetLocalPlayerTeleportData combined with :TeleportToPlaceInstance although it appears this method is deprecated.
sorry if i sound annoying and frankly, clueless, but how would i use :setteleportdata in an example script or just a block of code, the dev hub articles are pretty hard to understand to be honest
Wouldn’t Datastores and Subplaces be sufficient for this?
I see this same question get asked time and time again yet everyone gives near identical results which usually isn’t optimal.
Teleport Data is required if you don’t want select weapons to save (like if there’s unique weapons per level), other than that, datastores are completely fine to use.
And in-case that for some reason you’re using different EXPERIENCES altogether instead of different places, you should change it to use places instead as its far more optimal.
I’d argue they’re pretty easy to understand.
Here’s the specific article for SetTeleportData.
And here’s their code block:
local teleportOptions = Instance.new("TeleportOptions")
local teleportData = {
placeId = game.PlaceId,
jobId = game.JobId
}
teleportOptions:SetTeleportData(teleportData)
TeleportService:TeleportAsync(game.PlaceId, {player}, teleportOptions)
I read the docs completely wrong. Thanks @Chark_Proto for showing the correct usage of TeleportOptions. I updated my post (it still doesn’t use datastores but will be hacker proof).