How would I import datastores from a place to a place inside an experience?

Sorry if the title sounds confusing but I have multiple places inside my game, one place for the hub and others for the map/levels and I can’t figure out how to make my datastores and other features save over to the other places
Can someone help?
Thanks

1 Like

Should data be things or values?

It’s just that values ​​are easier to store than things

1 Like

Yeah it’s cash. I have it in the starter game but don’t know how to import it into the places.

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("im_eoin")



game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

   local Cash = Instance.new("IntValue", leaderstats)
   Cash.Name = "Cash"
	
	local tableOfData
	local success, errormessage = pcall(function()
		tableOfData = datastore:GetAsync(player.UserId)
	end)
	if success and tableOfData then -- we know that it is a returning player whom already played this game before
		for key, value in pairs(tableOfData) do
			print(key)
			print(value)
			Cash.Value = tableOfData["Cash"]
		end
	else -- we know that this player is new or their data got throttled (cached) or whatever you call it and have lost their data
		--sets the default value for new players
			
		Cash.Value = 0
		
	end
	
end)
game.Players.PlayerRemoving:Connect(function(player)
	
	local tableOfData = {
		["Cash"] = player.leaderstats.Cash.Value,
	}

	local success, errormessage = pcall(function()
		datastore:SetAsync(player.UserId, tableOfData) 
        print(tableOfData) --checking what is saved
	end)
end)

image
I highly recommend using the test on real servers, as the data will definitely be stored there

You can use StarterCash, NewCash (these are just cash names) according to your locations by making changes to this script.

Note:this script is universal, you can use pairs to store anything in the table

1 Like