How to save table to datastore and get it from other script

  1. What do you want to achieve? Keep it simple and clear!
    So basically i’m getting random gens depends on their chance, and i need to add amount of gens of player to table and save this table to datastore and then get it from script
  2. What is the issue? Include screenshots / videos if possible!
    Idk if it possible
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I don’t really wanna use 50 values for this

Here is random code and a line where should be adding a value:

local ST = game:GetService("ServerStorage")
local ChancesModule = require(ST.ChanceModule)
local Players = game.Players
local function GetRandomGenSet()
	local genSet = {}
	for genName, odds in pairs(ChancesModule) do
		if math.random() < 1/odds * 1000 then table.insert(genSet, genName) end
	end
	return genSet
end
 
local function InfiniteCall()
	-- Adding amount for gen 
	task.wait(1)
	InfiniteCall()
end

Players.PlayerAdded:Connect(function(plr)
	print("Plr Added")
	plr.CharacterAdded:Connect(function(Char)
		print("Char Added")
		local pHead = Char:FindFirstChild("Head")
		if pHead:FindFirstChild("AdvancedGameGUI") then
			InfiniteCall()
			print("Advanced GUI Found")
			
		else
			if pHead:WaitForChild("AdvancedGameGUI") then
				InfiniteCall()
			end
		end
	end)
	end)

DS script:

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

local function onPlayerJoined(player)

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

	local StatsFolder = Instance.new("Folder")
	StatsFolder.Name = "Stats"
	StatsFolder.Parent = player
	
	local PricesFolder = Instance.new("Folder")
	PricesFolder.Name = "PricesFolder"
	PricesFolder.Parent = player
	
	local PlayerGUIType = Instance.new("StringValue")
	PlayerGUIType.Name = "GUIType"
	PlayerGUIType.Parent = StatsFolder
	
	local PlayerGraphicsType = Instance.new("StringValue")
	PlayerGraphicsType.Name = "GraphicsType"
	PlayerGraphicsType.Parent = StatsFolder
	
	local FirstVisitValue = Instance.new("BoolValue")
	FirstVisitValue.Name = "GameVisited"
	FirstVisitValue.Parent = StatsFolder
	
	local PowerVal = Instance.new("NumberValue")
	PowerVal.Name = "Power"
	PowerVal.Parent = leaderstats
	
	local RebirthsAmountVal = Instance.new("NumberValue")
	RebirthsAmountVal.Name = "RebirthsAmount"
	RebirthsAmountVal.Parent = StatsFolder
	
	local RebirthPointsVal = Instance.new("NumberValue")
	RebirthPointsVal.Name = "RP"
	RebirthPointsVal.Parent = StatsFolder
	
	local PlayerLuck = Instance.new("NumberValue")
	PlayerLuck.Name = "PlrLuck"
	PlayerLuck.Parent = StatsFolder
	
	local IncLuckMoney = Instance.new("NumberValue")
	IncLuckMoney.Name = "IncLuckMoney"
	IncLuckMoney.Parent = StatsFolder

	local InCoalMoney = Instance.new("NumberValue")
	InCoalMoney.Name = "InCoalMoney"
	InCoalMoney.Parent = StatsFolder

	local IncRPMoney = Instance.new("NumberValue")
	IncRPMoney.Name = "IncRPMoney"
	IncRPMoney.Parent = StatsFolder
	
	local PlayerMoney = Instance.new("NumberValue")
	PlayerMoney.Name = "PlrMoney"
	PlayerMoney.Parent = StatsFolder
	
	local MoneyConvertMulti = Instance.new("NumberValue")
	MoneyConvertMulti.Name = "MonConvertMulti"
	MoneyConvertMulti.Parent = StatsFolder
	
	local MoneyConvertTimeout = Instance.new("NumberValue")
	MoneyConvertTimeout.Name = "MonConvertTimeout"
	MoneyConvertTimeout.Parent = StatsFolder
	
	
	local IncLuckMoneyPr = Instance.new("NumberValue")
	IncLuckMoneyPr.Name = "IncLuckMoneyPr"
	IncLuckMoneyPr.Parent = PricesFolder

	local InCoalMoneyPr = Instance.new("NumberValue")
	InCoalMoneyPr.Name = "InCoalMoneyPr"
	InCoalMoneyPr.Parent = PricesFolder

	local IncRPMoneyPr = Instance.new("NumberValue")
	IncRPMoneyPr.Name = "IncRPMoneyPr"
	IncRPMoneyPr.Parent = PricesFolder
	
	local CoalVal = Instance.new("NumberValue")
	CoalVal.Name = "CoalAmount"
	CoalVal.Parent = StatsFolder
	
	local IncLuckCoal = Instance.new("NumberValue")
	IncLuckCoal.Name = "IncLuckCoal"
	IncLuckCoal.Parent = StatsFolder


	local IncRPCoal = Instance.new("NumberValue")
	IncRPCoal.Name = "IncRPCoal"
	IncRPCoal.Parent = StatsFolder
	
	local IncPickSpeed = Instance.new("NumberValue")
	IncPickSpeed.Name = "InPickSpeed"
	IncPickSpeed.Parent = StatsFolder
	
	local IncLuckCoalPr = Instance.new("NumberValue")
	IncLuckCoalPr.Name = "IncLuckCoalPr"
	IncLuckCoalPr.Parent = PricesFolder
	
	local IncRPCoalPr = Instance.new("NumberValue")
	IncRPCoalPr.Name = "IncRPCoalPr"
	IncRPCoalPr.Parent = PricesFolder
	
	local IncPickSpeedPr = Instance.new("NumberValue")
	IncPickSpeedPr.Name = "IncPickSpeedPr"
	IncPickSpeedPr.Parent = PricesFolder
	
	local success, result = protectedCall(function()
		return dataStore:GetAsync("Data_"..player.UserId)
	end)

	if success then
		if result then
			if type(result) == "table" then
				PlayerGUIType.Value = result[1] or "Advanced"
				PlayerGraphicsType.Value = result[2] or "Low"
				FirstVisitValue.Value = result[3] or false
				PowerVal.Value = result[4] or 0
				RebirthsAmountVal.Value = result[5] or 0
				RebirthPointsVal.Value = result[6] or 0
				PlayerLuck.Value = result[7] or 1
				PlayerMoney.Value = result[8] or 0
				MoneyConvertMulti.Value = result[9] or 0.000000001
				MoneyConvertTimeout.Value = result[10] or 120
				IncLuckMoney.Value = result[11] or 1
				InCoalMoney.Value = result[12] or 1
				IncRPMoney.Value = result[13] or 1
				IncLuckMoneyPr.Value = result[14] or 100
				InCoalMoneyPr.Value = result[15] or 1000000
				IncRPMoneyPr.Value = result[16] or 10000000000000
				CoalVal.Value = result[17] or 0
 			end
		end
	else
		warn(result)
	end
end

local function onPlayerLeft(player)
	local success, result = protectedCall(function()
		return dataStore:SetAsync("Data_"..player.UserId, {player.Stats.GUIType.Value, player.Stats.GraphicsType.Value, player.Stats.GameVisited.Value,
			player.leaderstats.Power.Value, player.Stats.RebirthsAmount.Value, player.Stats.RP.Value, player.Stats.PlrLuck.Value, player.Stats.PlrMoney.Value,
			player.Stats.MonConvertMulti.Value, player.Stats.MonConvertTimeout.Value, player.Stats.IncLuckMoney.Value, player.Stats.InCoalMoney.Value,
			player.Stats.IncRPMoney.Value, player.PricesFolder.IncLuckMoneyPr.Value, player.PricesFolder.InCoalMoneyPr.Value, player.PricesFolder.IncRPMoneyPr.Value,
			player.Stats.CoalAmount.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.Stats.GUIType.Value, player.Stats.GraphicsType.Value, player.Stats.GameVisited.Value,
			player.leaderstats.Power.Value, player.Stats.RebirthsAmount.Value, player.Stats.RP.Value, player.Stats.PlrLuck.Value, player.Stats.PlrMoney.Value,
			player.Stats.MonConvertMulti.Value, player.Stats.MonConvertTimeout.Value, player.Stats.IncLuckMoney.Value, player.Stats.InCoalMoney.Value,
				player.Stats.IncRPMoney.Value, player.PricesFolder.IncLuckMoneyPr.Value, player.PricesFolder.InCoalMoneyPr.Value, player.PricesFolder.IncRPMoneyPr.Value,
				player.Stats.CoalAmount.Value
		})
		end)

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

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

Use

_G

as it refers to Global

ugh, but how do i get it from ds??

I recommend you read this Data Stores | Roblox Creator Documentation

especially the part about limits

if after reading that you still want to use datastore then this video will help show you how to save and load data to the datastore

I’m saving entire table of different values in datastore, but i need to add a table in table to save player gen amounts and get them lol