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:

2 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