Teleporting datastore values help

I am trying to make a money system so when a task is completed, you will be teleported back to the orginal place and get extra money

Here is the script for teleporting back with a new money

local rs = game:GetService("ReplicatedStorage")
local FinishedRemote = rs:WaitForChild("Finished")
local player = game.Players.LocalPlayer
local TeleportService = game:GetService('TeleportService')
local data = {}
FinishedRemote.OnClientEvent:Connect(function()
	player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 2500
	wait(1.5)
	data["Cash"] = player.leaderstats.Cash.Value
	script.Parent.Enabled = true
	wait(1.5)
	TeleportService:Teleport(10217679047, player, data)
end)

Leaderstats Script

game.Players.PlayerAdded:Connect(function(Player)

	local leaderstats = Instance.new("Folder",Player)
	leaderstats.Name = "leaderstats"

	local Credits = Instance.new("NumberValue",leaderstats)
	Credits.Name = "Cash"
	if Credits.Value ~= 0 then 
		-- has more or less than 0
	else
		Credits.Value = 0
	end
end)

Teleport to place script

local data = {}
local Players = game.Players
local TeleportService = game:GetService("TeleportService")
data["Cash"] = player.leaderstats.Cash.Value

TeleportService:TeleportToPrivateServer(id, Players, data)

It teleports the players fine the problem but the cash stuff just stays the same, I tryed making a new datastore and the place ID but with no luck

I dont see the script that handles the Datastore, without it, I dont know where is the issue.

Everytime a player join your game, you should read the datastore to know how much money you should apply to the player. Then Teleport somewhere else, when joining that new place, again, read the DataStore to apply money, then player collect more money, now, before teleporting the player, Save the Datastore with the new money, then Teleport the player, now when player joins original place read the DataStore again, and you will find the new money player obtained from the other place.

Without the Datastore it will never work.

Im assuming your talking about the Datastore script that saves existing data? Here is the script

local dataStoreService = game:GetService("DataStoreService")
local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("leaderstats")

local loaded = {}

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	if player.UserId > 0 and player.Parent then
		local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId)
		if leaderstatsData ~= "Request rejected" then
			if leaderstatsData then
				for i, stat in ipairs(leaderstats:GetChildren()) do
					local value = leaderstatsData[stat.Name]
					if value then
						stat.Value = value
					end
				end
			end
			loaded[player] = true
		end
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		if loaded[player] then
			local leaderstatsData = {}
			for i, stat in ipairs(leaderstats:GetChildren()) do
				leaderstatsData[stat.Name] = stat.Value
			end
			leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData)
		end
	end
	loaded[player] = nil
end)




Firstly, I assume that the place you’re teleporting to and from are in the same “Universe” and not 2 seperate games completely, right?

Secondly, you should wrap your GetAsync in a pcall and check if it was successful, and use return leaderstatsDataStore:GetAsync(player.UserId).

Why you using GlobalDataStore? In your case I think thats not needed, would be easier if you use normal DataStore with name as you are doing “leaderstats”

The fact that you are using GlobalDataStore makes me think that you are trying to write/read a DataStore across different games? If you pretend to use a DataStore across Places which are inside the same Game, using a normal DataStore is enough; if you pretend to write/read data from the same DataStore in Game1 and in Game2, then that wont work. You would need an external server using Http requests to share data across different games.

If the 2 places are inside the same game then try with this, delete this script:
image

And replace your current DataStore script with this:

local dataStoreService = game:GetService("DataStoreService")
local leaderstatsDataStore = dataStoreService:GetDataStore("leaderstats")

local loaded = {}

game.Players.PlayerAdded:connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

	local Credits = Instance.new("NumberValue")
	Credits.Name = "Cash"
	Credits.Parent = leaderstats
	Credits.Value = 0

	leaderstats.Parent = player
	
	local succ, data = pcall(function()
		return leaderstatsDataStore:GetAsync(player.UserId)
	end)

	if succ then
		if data then
			warn("Data from player exist")
			print(data)
			for i, stat in ipairs(leaderstats:GetChildren()) do
				local value = data[stat.Name]
				if value then
					stat.Value = value
				end
			end
		else
			warn("No data in DSS, new player")
		end
		loaded[player] = true
	else
		warn("Datastore connection failed")
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		if loaded[player] then
			local leaderstatsData = {}
			for i, stat in ipairs(leaderstats:GetChildren()) do
				leaderstatsData[stat.Name] = stat.Value
			end
			warn("Data to save")
			print(leaderstatsData)
			local succ, err = pcall(function()
				return leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData)	
			end)
			if not succ then
				warn(err)
			end
		end
	end
	loaded[player] = nil
end)