How do i share value with datastore across places in one game/universe(Second topic)

  1. What do you want to achieve? Keep it simple and clear!
    So i need to share a datastore value between places in one game/universe, i already made topic about that, but no one gave me a normal explanation which line i should add to my script in main game
  2. What is the issue? Include screenshots / videos if possible!
    I looked a lot of topics, but didn’t understand anything
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes i looked, but i didn’t understand anything

Other Place Script:

local playersService = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local dataStore = dataStores:GetDataStore("DataStore")

local protectedCall = pcall

local function onPlayerJoined(player)
	
	local SecretStatsFolder = Instance.new("Folder")
	SecretStatsFolder.Name = "SecretStats"
	SecretStatsFolder.Parent = player
	
	local watershard = Instance.new("BoolValue")
	watershard.Name = "WaterShard"
	watershard.Parent = SecretStatsFolder
	
	local success, result = protectedCall(function()
		return dataStore:GetAsync("Data_"..player.UserId)
	end)

	if success then
		if result then
			if type(result) == "table" then
			watershard.Value = result[1] or false
			end
		end
	else
		warn(result)
	end
end

local function onPlayerLeft(player)
	local success, result = protectedCall(function()
		return dataStore:SetAsync("Data_"..player.UserId, {player.SecretStats.WaterShard.Value})
	end)

	if success then
		print(result)
			print("Everything success")
	else
		warn(result)
		warn("everything wrong")
	end
end

local function onServerClosed()
	for _, player in ipairs(playersService:GetPlayers()) do
		local success, result = protectedCall(function()
			return dataStore:SetAsync("Data_"..player.UserId, {player.SecretStats.WaterShard.Value})
		end)

		if success then
			print(result)
		else
			warn(result)
		end
	end
end

playersService.PlayerAdded:Connect(onPlayerJoined)
playersService.PlayerRemoving:Connect(onPlayerLeft)
game:BindToClose(onServerClosed) 

Main Script:

local playersService = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local dataStore = dataStores:GetDataStore("DataStore")

local protectedCall = pcall

local function onPlayerJoined(player)
	local StatsFolder = Instance.new("Folder")
	StatsFolder.Name = "Stats"
	StatsFolder.Parent = player
	
	local SecretStatsFolder = Instance.new("Folder")
	SecretStatsFolder.Name = "SecretStats"
	SecretStatsFolder.Parent = player
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local clicks = Instance.new("IntValue")
	clicks.Name = "Clicks"
	clicks.Parent = leaderstats

	local money = Instance.new("NumberValue")
	money.Name = "Money"
	money.Parent = leaderstats

	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	
	local memory = Instance.new("IntValue")
	memory.Name = "Memory"
	memory.Parent = StatsFolder
	
	local memorylevel = Instance.new("IntValue")
	memorylevel.Name = "MemoryLevel"
	memorylevel.Parent = StatsFolder
	
	local mouselevel = Instance.new("IntValue")
	mouselevel.Name = "MouseLevel"
	mouselevel.Parent = StatsFolder
	
	local rcamount = Instance.new("IntValue")
	rcamount.Name = "RCAmount"
	rcamount.Parent = StatsFolder
	
	local rcmultimoney = Instance.new("NumberValue")
	rcmultimoney.Name = "RCMultiMoney"
	rcmultimoney.Parent = StatsFolder
	
	local rcmulticlicks = Instance.new("NumberValue")
	rcmulticlicks.Name = "RCMultiClicks"
	rcmulticlicks.Parent = StatsFolder
	
	local rc1collected = Instance.new("BoolValue")
	rc1collected.Name = "RC1Col"
	rc1collected.Parent = StatsFolder
	
	local rc2collected = Instance.new("BoolValue")
	rc2collected.Name = "RC2Col"
	rc2collected.Parent = StatsFolder
	
	local moneyfromtrade = Instance.new("NumberValue")
	moneyfromtrade.Name = "MoneyFT"
	moneyfromtrade.Parent = StatsFolder
	
	local clicksperclick = Instance.new("NumberValue")
	clicksperclick.Name = "ClicksPerClick"
	clicksperclick.Parent = StatsFolder
	
	local leafstat = Instance.new("BoolValue") -- leaf boss
	leafstat.Name = "LeafStat"
	leafstat.Parent = SecretStatsFolder
	
	local watershard = Instance.new("BoolValue")
	watershard.Name = "WaterShard"
	watershard.Parent = SecretStatsFolder
	
	local cookiestat = Instance.new("BoolValue")
	cookiestat.Name = "CookieStat"
	cookiestat.Parent = SecretStatsFolder
	local success, result = protectedCall(function()
		return dataStore:GetAsync("Data_"..player.UserId)
	end)

	if success then
		if result then
			if type(result) == "table" then
				clicks.Value = result[1] or 0
				money.Value = result[2] or 0
				rebirths.Value = result[3] or 0
				memory.Value = result[4] or 10
				memorylevel.Value = result[5] or 0
				mouselevel.Value = result[6] or 0
				rcamount.Value = result[7] or 0
				rcmultimoney.Value = result[8] or 1
				rc1collected.Value = result[9] or false
				moneyfromtrade.Value = result[10] or 1
				rcmulticlicks.Value = result[11] or 1
				clicksperclick.Value = result[12] or 1
				rc2collected.Value = result[13] or false
				leafstat.Value = result[14] or false
				watershard.Value = result[15] or false
				cookiestat.Value = result[16] or false
			end
		end
	else
		warn(result)
	end
end

local function onPlayerLeft(player)
	local success, result = protectedCall(function()
		return dataStore:SetAsync("Data_"..player.UserId, {player.leaderstats.Clicks.Value, player.leaderstats.Money.Value, player.leaderstats.Rebirths.Value, player.Stats.Memory.Value, player.Stats.MemoryLevel.Value, player.Stats.MouseLevel.Value, player.Stats.RCAmount.Value, player.Stats.RCMultiMoney.Value, player.Stats.RC1Col.Value,player.Stats.MoneyFT.Value, player.Stats.RCMultiClicks.Value, player.Stats.ClicksPerClick.Value, player.Stats.RC2Col.Value, player.SecretStats.LeafStat.Value, player.SecretStats.WaterShard.Value, player.SecretStats.CookieStat.Value})
	end)

	if success then
		print(result)
			print("Everything success")
	else
		warn(result)
		warn("everything wrong")
	end
end

local function onServerClosed()
	for _, player in ipairs(playersService:GetPlayers()) do
		local success, result = protectedCall(function()
			return dataStore:SetAsync("Data_"..player.UserId, {player.leaderstats.Clicks.Value, player.leaderstats.Money.Value, player.leaderstats.Rebirths.Value, player.Stats.Memory.Value, player.Stats.MemoryLevel.Value, player.Stats.MouseLevel.Value, player.Stats.RCAmount.Value, player.Stats.RCMultiMoney.Value, player.Stats.RC1Col.Value,player.Stats.MoneyFT.Value, player.Stats.RCMultiClicks.Value, player.Stats.ClicksPerClick.Value, player.Stats.RC2Col.Value, player.SecretStats.LeafStat.Value, player.SecretStats.WaterShard.Value, player.SecretStats.CookieStat.Value})
		end)

		if success then
			print(result)
		else
			warn(result)
		end
	end
end

playersService.PlayerAdded:Connect(onPlayerJoined)
playersService.PlayerRemoving:Connect(onPlayerLeft)
game:BindToClose(onServerClosed) 

I looked 5 more topics about that, but i do not understand still…

Datastore service is saved by the player data inside the universe, meaning that you have to paste both of the datastore scripts inside the serverscriptservice inside each place. It automatically saves in the universe, so if you have more places to share the data with, it will always share the data from a game to another by universe logic. Keep in mind, any games out of the universe the data store is saved in won’t work.

You have to paste the same script in the ServerScriptService in the other place as you did in the main game.

Bro, answering one year topic is useless

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.