Help with Incrementing dictonary using Datastore 2

This seems like a very easy question to answer, although I cannot find the answer. I have this big dictonary:(ignore the achievements and inventory section, since I’m gonna edit that later on).

local function SetDataTable()	
	local UserData = {
		MainStats = {-- Coins, Gems, Level, XP, are listed in here
			["Coins"] = 0,
			["Gems"] = 0,
			["Level"] = 1,
			["XP"] = 0
		},
	--[[	Achievements = {--Stars in here
		},
		Inventory = {-- Trails and Titles
		},]]--
		CourseStats = {--Number of Coins and Gems collected in courses, Time, and Deaths
			["LobbyCoins"] = 0,
			["LobbyGems"] = 0
		
		}
	}
	return UserData
end

Lobby Coins are how many coins you collected in the lobby.
Now with my coin handler I cannot increment the dictionary number. My question is how do you do that?

lobbycoins:Increment(1)

Here is the full script:(all you really need to pay attention to is the lobbyCoins:Increment part. The rest works fine).

-- Coin handler
local DataStore2 = require(script.Parent.DataStore2);

game.Players.PlayerAdded:Connect(function(player)
	
	local lobbycoins = DataStore2("LobbyCoins", player);
	local coins = lobbycoins:Get();
	
	if(coins == nil) then
		coins = {};
		lobbycoins:Set(coins);
	end
	game.ReplicatedStorage.RemoteEvents.coinManagerEvent:FireClient(player,coins);
	
end);


game.ReplicatedStorage.RemoteEvents.coinGatherEvent.OnServerEvent:Connect(function(player, coinID)
	local coin;
	local coins = game.Workspace.Coins:GetChildren();
	
	for k,v in pairs(coins) do
		
		if(v.CoinID.Value == coinID) then
			coin = v;
			break;
		end
		
	end
	
	if(coin == nil) then
		print("Not a valid coin!");
	else
	
		print(player:DistanceFromCharacter(coin.Position));
		if(player:DistanceFromCharacter(coin.Position) < 35.0) then
			
			
			local lobbycoins = DataStore2("LobbyCoins", player);
			local temp = lobbycoins:Get()
			table.insert(temp, coinID);
			lobbycoins:Set(temp);
			
			local coinStore = DataStore2("Coins", player);
			local XPStore = DataStore2("XP",player);

			
			if(coinStore:Get() == nil) then
				coinStore:Set(0);
			end
			
			lobbycoins:Increment(1)
			XPStore:Increment(1);
			coinStore:Increment(coin.CoinValue.Value);
			
		else
			print("Player isn't near the coin!");
		end
	end
	
end)

This script was created by a friend of mine who was helping me at first, but can’t anymore due to real life issues. I get what the whole script does, I just can’t figure out why he put semicolons after each line of code. What does it do? Is it required?

Have a great day! :grinning:

The way you increment values in a table, basically, you make a copy of the table, set the values in that copy, and then set the original table to the copy.

Here’s a function that increments your DataStores LobbyCoins value. I made this on the spot so i dont know if this works. You should change the Key variable to the key of your Data Table.

local DataKey = "Key"

function IncrementLobbyCoins(Player, Value)
	--We get the players data
	local UserData = DataStore2(DataKey, Player)
	
	--We get a copy of the data
	local Table = UserData:Get(SetDataTable())
	
	--Next we increment the value we want
	Table.CourseStats["LobbyCoins"] = Table.CourseStats["LobbyCoins"] + Value
	
	--Now we set the userdata to the copy of the table we changed
	UserData:Set(Table)
	
end
6 Likes

great, thank you!

Also do you know why the semicolons are at the end?

They don’t change the script. It’s just an indicator that the line ends there. They sometimes use these in the Command Bar.

ok, thank you!

(30Charrrrrrrrrrrrrs)

Sorry to bother you again, but local Table = UserData:Get(SetDataTable()) has a blue line under it(under SetDataTable). I forgot to mention early that I combined this tables right before the SetDataTable() function.

DataStore2.Combine(MainKey, "MainStats","CourseStats"--[[, "Achievements","Inventory"]])

Does this have anything to do with it. I have tried switching up your script but it still doesn’t work.

SetDataTable() should be the function that returns the Default Data Table you set. This is so that if the player does not have any data set, it will get the default data table instead.

If you have any problems message me.