Need Help With Making a Data Module With DS2

Hi, I’m trying to make a module script that can handle all of the players’ data for my game. However, my current script doesn’t seem to work at all. Here is a snippet of code that I’m having my main problem with. This is my first module script ever too.

DataStore2 = require(game.ServerScriptService.DataStore2)
rs = game:GetService('ReplicatedStorage')

local data = {}

Default = {
Coins = 1000,
UnlockedZones = "Town"
}


data.coinvalues = {
	Coin = 1, 
	RedCoin = 5, 
	BlueCoin = 10, 
	GreenCoin = 100, 
	PinkCoin = 500,
	DiamondCoin = 1000
}

data.zones = "Town Desert Factory Forest Mountain Snow Swamp Teepot"



function data.LoadData(player)
	local Datastore = DataStore2("PlayerData2", player)
	local PlayerData = Datastore:Get(Default)

	data.Coins = 1000
	data.UnlockedZones = "Town"

	local function coinsUpdated(updatedVal)
		Coins = Datastore:Get(updatedVal)
	end

	local function zonesUpdated(updatedVal)
		UnlockedZones = Datastore:Get(updatedVal)
	end

	coinsUpdated(Default)
	Datastore:OnUpdate(coinsUpdated)

	zonesUpdated(Default)
	Datastore:OnUpdate(zonesUpdated)
-- i havent worked on anything below this
	for i, v in pairs(PlayerData) do
		pcall(function()
			Config:SetAttribute(i, v)
			Config:GetAttributeChangedSignal(i):Connect(function()
				PlayerData[i] = Config:GetAttribute(i)
			end)
		end)
	end

	--Config.Parent = player
end

I’ve tried just about everything I can think of, but nothing has worked. Any help is appreciated!

1 Like

By doesn’t work, do you mean that it doesn’t save or that it doesn’t load? Or does it not correctly convert your values into a string or…?

oh sorry yeah i was pretty blank about that, alright so i meant that i cant seem to get the coins value, it returns as nil and i also wanted to know if i even made this script correctly.

Coins = Datastore:Get(updatedVal)

Noticing here that you are actually defining a new variable, Coins, which is not accessed anywhere else in the code.

Did you perhaps mean data.Coins?

1 Like

Sorry about the delay but thanks for pointing that out, however I still cant seem to access the players coins. Maybe I just don’t know how module scripts work but this is the code that won’t get it

local data = require(game.ServerScriptService.DataManager)

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	local pdata = data.LoadData(player)
	print(data.Coins)
end)