How to save multiple values with DataStore2 module

Read the topic name. If you have, please help me solve the issue.

Now I know how to do this with regular DataStore, but not with this module.

This is my script so far, and I’m not even sure whether I’m doing it right up to this point.

local datastore = require(1936396537)
local saveData = datastore.Combine("Data", "minigamesPlayed", "wins")

game.Players.PlayerAdded:Connect(function(p)
	
	local WINS = datastore("wins", p)
	local GAMES = datastore("minigamesPlayed", p)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = p
	
	local gamesplayed = Instance.new("NumberValue")
	gamesplayed.Name = "Games Played"
	gamesplayed.Parent = leaderstats
	
	local wins = Instance.new("NumberValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
end)

Does anyone know how to do it?

2 Likes

well u are doing it right, now u need to set up some update functions, watch the video:

2 Likes

I’ve used DataStore2 in a game before, but my question is how can I save multiple values.

1 Like

you mean different types of values (num, string, bool)?

1 Like

No, I want to save two NumberValues.

1 Like

well in that case, just watch a full video… and keep in mind, at two or more values u need two or more update functions

2 Likes

From what you’re asking I’m assuming you want to save an array/table? Each index is a number value as you said. If this is the case you’ll want to use DataStore2’s :GetTable() function. Essentially, you’ll have your two number values in a table, like so,

local NumberValues = {1.1, 2}

or

local NumberValues = {
    [1] = 1.1,
    [2] = 2
}

Then in your DataStore2’s combine just add a new DataStore. Then proceed with the following in your script,

local NumberValues = {
    [1] = 1.1,
    [2] = 2
}
local NUMBERVALUES = datastore("num", p)
---
local function UpdateNum(UpdatedValue)
    local tab = NUMBERVALUES:GetTable(UpdatedValue)
end

UpdateNum(NumberValues) -- update with the default table
NUMBERVALUES:OnUpdate(UpdateNum) -- when the data is updated it'll do this

Now you also make it seem as though you’re not sure how to save your wins & minigames played? Well if this is also the case you’re on the right track.

local datastore = require(1936396537)
local saveData = datastore.Combine("Data", "minigamesPlayed", "wins")

game.Players.PlayerAdded:Connect(function(p)	
	local WINS = datastore("wins", p)
	local GAMES = datastore("minigamesPlayed", p)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = p
	
	local gamesplayed = Instance.new("NumberValue")
	gamesplayed.Name = "Games Played"
	gamesplayed.Parent = leaderstats
	
	local wins = Instance.new("NumberValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats

    local function UpdateWins(UpdatedValue)
        wins.Value = WINS:Get(UpdatedValue)
    end
    local function UpdatePlayed(UpdatedValue)
        gamesplayed.Value = GAMES:Get(UpdatedValue)
    end

    UpdateWins(0) -- zero being the default value
    UpdatePlayed(0)
    WINS:OnUpdate(UpdateWins)
    GAMES:OnUpdate(UpdatePlayed)
end)

I hope this help and the DataStore2 API and documentation can be found here
If you want to test this in studio and not in-game put a BoolValue in ServerStorage and name it “SaveInStudio” and make it true.

4 Likes

Ok, here is a script that adds the things.

local module = require(game.ReplicatedStorage.GlobalFunctions)
local datastore = require(1936396537)
local saveData = datastore.Combine("Data", "minigamesPlayed", "wins")

game.ServerStorage.StartMinigame.Event:Connect(function(minigame)
	if minigame == "Sword Fight" then
		script.Parent.Sound:Play()
		local plrs = {}
		for i, p in pairs(game.Players:GetPlayers()) do
			local char = p.Character.HumanoidRootPart
			char.Position = script.Parent.SwordFightSpawn.Position + Vector3.new(math.random(-50.314 / 2, 50.314 / 2), 0, math.random(-49.3 / 2, 49.3 / 2))
			table.insert(plrs, p)
		end
		local newThread = coroutine.create(function()
			game.Players.PlayerRemoving:Connect(function(p)
				if (#plrs > 0) then
					local item = module.finditem(plrs, p)
					table.remove(plrs, item)
				end
			end)
			while (#plrs > 0) do
				for i, plr in pairs(plrs) do
					if plr.Character.Humanoid.Health <= 0 then
						plr.Backpack:ClearAllChildren()
						local item = module.finditem(plrs, plr)
						table.remove(plrs, item)
					end
				end
				wait(0.5)
			end
		end)
		coroutine.resume(newThread)
		for i = 10, 1, -1 do
			game.ReplicatedStorage.Text:FireAllClients("In "..tostring(i).." seconds, you will have to kill all opponents with a sword.")
			wait(1)
		end
		for i, pl in pairs(plrs) do
			local sword = game.ServerStorage.MinigameTools["1. Sword Fight Sword"]:Clone()
			sword.Parent = pl.Backpack
			local gamesPlayed = datastore("minigamesPlayed", pl)
			gamesPlayed:Increment(1, 0)
		end
		local val = 60
		repeat
			game.ReplicatedStorage.Text:FireAllClients("You have "..tostring(val).." seconds left to kill all opponents.")
			val = val - 1
			wait(1)
		until #plrs <= 1 or val == 0
		if #plrs <= 1 then
			game.ReplicatedStorage.Text:FireAllClients(plrs[1].Name.." has defeated all opponents!")
			local wins = datastore("wins", plrs[1])
			wins:Increment(1, 0)
		end
		if val == 0 then
			game.ReplicatedStorage.Text:FireAllClients("Time has ran out!")
		end
		for i, player in pairs(plrs) do
			local item = module.finditem(plrs, player)
			table.remove(plrs, item)
		end
		wait(3)
		script.Parent.Sound:Stop()
		game.ServerStorage.BeginIntermission:Fire()
		wait(1)
		script.Parent.Parent = game.ServerStorage.Minigames
	end
end)

And for some reason, it says it adds to the queue,and I should send requests less. Are you sure this properly uses the module?

Ok, I’ve decided not to do it because it REALLY messes the game up, so thanks regardless, but I’ve decided not to. Thank you anyways!