Moving Data Between Games

So as I’ve been making a tower defence game myself, I’ve come to a problem on how I can transfer what tower the player has equipped from the lobby game to the action game.

The reason for this topic is so I can start off easy and ask, how can I move a player’s data (like multiple IntValues in a file) into a different game?

More Info
In tower defence games (for what I’ve seen), there are values in a folder under the local players name and that gets transferred into the game that the player teleports to and I wanted to know how I can do that.

1 Like

Ok so, What your looking for is using the “Teleport Service”.
Teleport Service is used for moving a Player instance between places around the Roblox platform.

You will have to make something like this:

The first script (Probably in the lobby) will teleport the Player.

local TeleportService = game:GetService("TeleportService")

local TPData = {
    Money = 123
}
 
local TeleportOptions = Instance.new("TeleportOptions")

TeleportOptions:SetTeleportData(TPData)

pcall(function() -- We would like to teleport the player with a "Protected Call" function, Because like any call that involves network requests, They have a possibility to fail. 
    TeleportService:Teleport(PlaceID, Player, TeleportOptions)
end)

And another script inside the Tower Defense game itself:

game:GetService("Players").PlayerAdded:Connect(function(Player) -- Fires whenever a Player has joined.
    local Data = Player:GetJoinData() -- Reads the JoinData.
    local TPData = Data.TeleportData
    local Money = TPData.Money
 
    print(Money)
end)

Hope it helped! :grin:

If you are interested in going more deeply inside you can reach out for more information here:

4 Likes

When moving entire files with values inside, do we just insert their location in the TPData table?

For example:

local player = game.Players.PlayerAdded
local TPData = {
    player.(insert file location here)
}

No, You can’t transfer any instance as an entire data unless you will create a new one in the other place.
What you can do is insert its data.

Example:

--[[SCRIPT]]--
--// Let's say that a remote will teleport the player. \\--

--// Services:
local ReplicatedStorage = game:GetService("ReplicatedStorage ")
local TeleportService = game:GetService("TeleportService")

--// For example:
local IntValue = Instance.new("IntValue", workspace)
IntValue.Name = "Money"
IntValue.Value = 10

--// Functions
local function OnFireRemote(Player)
  local TPData = {
    Money = IntValue.Value -- Or you can just type: workspace.Money.Value
    Name = IntValue.Name -- Or again you can just type: workspace.Money.Name
  }
 
  local TeleportOptions = Instance.new("TeleportOptions")
  TeleportOptions:SetTeleportData(TPData)

  pcall(function() -- We would like to teleport the player with a "Protected Call" function Because, like any call that involves network requests, They have a possibility to fail. 
    TeleportService:Teleport(PlaceID, Player, TeleportOptions)
  end)
end

If you are a bit confused, You can check this out:

So I’ve been analyzing some scripts on a tower defence game and saw these scripts

Server Scripts

local TowerInventoryData = game:GetService("DataStoreService"):GetDataStore("TowerInventory3")

function save(Player)
	local saveData = {}
	for i, v in pairs(Player:WaitForChild("TowerInventory"):GetChildren())do
		saveData[#saveData + 1] = v.Name
	end
	TowerInventoryData:SetAsync(Player.UserId, saveData)
end

game.Players.PlayerAdded:Connect(function(Player)
	
	local folder = Instance.new("Folder", Player)
	folder.Name = "TowerInventory"
	
	local success, errorm = pcall(function()
		local load = TowerInventoryData:GetAsync(Player.UserId)
		if load then
			for i, v in pairs(load)do
				local x = Instance.new("IntValue", folder)
				x.Name = v
			end
		end
	end)
end)

And when you equip a tower, it renames an IntValue to the name of the tower

And in the other game where it loads that data:
ServerScript

local TowerInventoryData = game:GetService("DataStoreService"):GetDataStore("TowerInventory3")

function save(Player)
	
	local saveData = {}
	
	for i, v in pairs(Player:WaitForChild("TowerInventory"):GetChildren())do
		saveData[#saveData + 1] = v.Name
	end
	
	TowerInventoryData:SetAsync(Player.UserId, saveData)
	
end

game.Players.PlayerAdded:Connect(function(Player)
	
	local folder = Instance.new("Folder", Player)
	folder.Name = "TowerInventory"
	
	local success, errorm = pcall(function()
		
		local load = TowerInventoryData:GetAsync(Player.UserId)
		if load then
			for i, v in pairs(load)do
				local x = Instance.new("IntValue", folder)
				x.Name = v
			end
		end
		
	end)
	
end)

I don’t understand how exactly this all works unless there are more to it than just these 2 scripts

(No I did not hack to get these scripts I just found a not so well working open source game)

1 Like

Hello, i know i’m kinda late… I mean not kinda, i am 2 years late… But i still wanna help if you didn’t find solution and didn’t abandone the project or even roblox.

So, what this person said is that you use teleport service to teleport players, but you also need to put data inside which can’t be an instance. In the tower game itself, when player joins you get player data you sent from the lobby.

So what are you gonna do in the lobby script, is that you should get all your data and put it in table, and then teleport it with player.

Here’s the teleportation script

local TeleportService = game:GetService("TeleportService")

--Put all needed data in this table just like here:

local TPData = {
    Tower1 = Player.Tower1.Value,
    Tower2 = Player.Tower2.Value,
    Tower3 = Player.Tower3.Value,
    Tower4 = Player.Tower4.Value,
}
 
local TeleportOptions = Instance.new("TeleportOptions")

TeleportOptions:SetTeleportData(TPData)

pcall(function() -- We would like to teleport the player with a "Protected Call" function, Because like any call that involves network requests, They have a possibility to fail. 
    TeleportService:Teleport(PlaceID, Player, TeleportOptions)
end)

Now you teleported player, so we are going to the main game script.

game:GetService("Players").PlayerAdded:Connect(function(Player) -- Fires whenever a Player has joined.
    local Data = Player:GetJoinData() --Now we are getting the table we sent before
    local TPData = Data.TeleportData
    local Tower1Name = TPData.Tower1 --And you do the same with tower 1, tower 2 and so on. Just make sure that you didn't put instance in the table and you are using right names of the values inside of it.
 
    print(Tower1)
end)

Hope i could explain to you how to use it. Have in mind that i used @CFramedDev script, i just wanted to try to explain you little bit better how to use it. If it helped you make sure to check solution on him, not me. (beacuse i didn’t write the script, i just changed it little bit for your case hoping you can understand it better)

1 Like