Data Module not working

Hey! I’ve been working on a data module, and it’s using local functions and then a module function which calls the local functions. But, it is erroring when I’m calling the functions.
This is the code:

local LvlDS = DataStoreService:GetDataStore("LvlStats")
local CrownDS = DataStoreService:GetDataStore("Crowns")
local GemsDS = DataStoreService:GetDataStore("Gems")

local module = {}

local function GetLvlInteger(plr)
	local success, err = pcall(function()
		local data = LvlDS:GetAsync(plr.UserId)
	    return data
	end)
	if err then warn("DATA FAIL: "..err)
end

local function Leaderstats(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"
	
	local crowns = Instance.new("IntValue")
	crowns.Name = "Crowns"
	crowns.Parent = leaderstats
	
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Parent = leaderstats			
end

local function Decode(LvlData,Lvl)
	if LvlData == 0 then 
		Lvl.Value = "Noob"
	elseif LvlData == 1 then
		Lvl.Value = "Learner"
	elseif LvlData == 2 then
		Lvl.Value = "Apprentice"
	elseif LvlData == 3 then
		Lvl.Value = "Swordsmen"
	elseif LvlData == 4 then
		Lvl.Value = "Novice"
	elseif LvlData == 5 then
		Lvl.Value = "Expert"
	elseif LvlData == 6 then
		Lvl.Value = "Master"
	elseif LvlData == 7 then
		Lvl.Value = "Ruler"
	elseif LvlData == 8 then
		Lvl.Value = "Legendary"
	elseif LvlData == 9 then
		Lvl.Value = "God"
	end
end
	
local function CodeUp(LvlData)
if LvlData == "Noob"then
return 0 
elseif LvlData == "Learner"then
return 1
elseif LvlData == "Apprentice"then
return 2
elseif LvlData == "Swordsmen"then
return 3
elseif LvlData == "Novice"then
return 4
elseif LvlData == "Expert"then
return 5
elseif LvlData == "Master"then
return 6
elseif LvlData == "Ruler"then
return 7
elseif LvlData == "Legendary"then
return 8 
elseif LvlData == "God"then
return 9
end
end 
end

	
local function GetCrowns(plr,crownsObj)	
	local success, err = pcall(function()
		local Data = CrownDS:GetAsync(plr.UserId)
	    if Data then crownsObj.Value = Data end
	end) 
	if err then warn("DATA FAIL:"..err)end
end

local function SetCrowns(plr,crownsObj)
	local success, err = pcall(function()
		local Data = plr.leaderstats.Crowns.Value
	    CrownDS:SetAsync(plr.UserId, Data)
	end)	
	if err then warn("DATA FAIL:"..err)end	
end

local function GetGems(plr,gemsObj)
	local success, err = pcall(function()
		local Data = GemsDS:GetAsync(plr.UserId)
	if Data then gemsObj.Value = Data end
	end)		
	if err then warn("DATA FAIL:"..err)end
	if success then end
end

local function SetGems(plr,gemsObj)
	local success, err = pcall(function()
		local Data = plr.leaderstats.Gems.Value	
	    GemsDS:SetAsync(plr.UserId, Data)
	end)
	if err then warn("DATA FAIL:"..err)end
	if success then end 
end

local function setLevelInteger(plr,lvl)
	local success, err = pcall(function()
		local Data = lvl
		LvlDS:SetAsync(plr.UserId,Data)
	end)
	if err then warn("DATA FAIL: "..err) 
end
	
function module.PlayerJoin(plr)
	Leaderstats(plr)
	local gems = plr.leaderstats.Gems
	local crowns = plr.leaderstats.Crowns
	local Lvl = plr.leaderstats.Level	
		
	local LvlIntegerFromSave = GetLvlInteger(plr)
	Decode(Lvl,LvlIntegerFromSave)		
	GetCrowns(plr,crowns)	
	GetGems(plr,gems)	
		
end	
	
function module.PlayerLeft(plr)
		
end
end		
	
return module

It’s saying the local functions are unknown for some reason.

When you’re getting the data you have to return something.

1 Like

as Seoarro said, you appear to be missing some return statements in some of your get functions.

Ohh sorry, I re-wrote the module, and posted the new one here, which I forget to do some stuff in. But that isn’t my problem @Seoarro @Nymlet . It’s not doing anything, I’ve debugged, wrapped in pcall. The problem is it’s not calling functions, because it’s saying Unknown Symbol when I’m calling a few of the local functions. There’s no output or anything.

image

aha alright. Well lets try to debug that then. Are you sure you’re requiring the module before attempting to call any of the functions inside? I assume you are, just making sure :slight_smile:

Unknown symbol tells me that there is a syntax error which might be messing it up for you, have you checked?

1 Like

This is the code when I’m requiring it with the player added event.

local DM = require(script.DataModule)

game.Players.PlayerAdded:Connect(function(plr)
	DM.PlayerJoin(plr)
end)

Oh! I saw the image just now. Can you post the code for that file? Leaderstats is not a LUA global so im guessing you forgot to define it.

DataModule.lua (3.2 KB)
That is a saved version of the module, just without the event.

Okay so the problem is, whenever you require a module the locally instantiated functions cannot be called.

You could work around this by making all the functions a property of the module like this:

module.Leaderstats = function(plr)
	local leaderstats = Instance.new("Folder")
	[[rest of your code]]		
end

and then calling it liked this:

function module.PlayerJoin(plr)
	module.Leaderstats(plr)
	[[rest of your code]]	
end

That should at least get rid of the error and run the code.

So should I make a seperate module that has the local functions in, then I can make the original data module require the module with the local functions as module functions?

I would suggest not making them local, and adding them to the module like in the example above :slight_smile:

1 Like

Hey! It’s still not working no Idea why. Here is the code:

--Event handler
local DM = require(script.DataModule)

game.Players.PlayerAdded:Connect(function(plr)
	DM.PlayerJoined(plr)
end)
--Initial data module--
local module = {}
local LFL = require(script.LocalFuncLibrary)
function module.PlayerJoined(plr)		
	LFL.Leaderstats(plr)
	local Leaderstats = plr:FindFirstChild("leaderstats")
	local gems = Leaderstats.Gems
	local crowns = Leaderstats.Crowns
	local rank = Leaderstats.Rank
	
	local LvlInteger = LFL.GetLevelInteger(plr)
	local GemsInteger = LFL.GetGems(plr)
	local CrownsInteger = LFL.GetCrowns(plr)
	
	local DecodedLvlString = LFL.Decode(LvlInteger)
	
	gems.Value = GemsInteger
	crowns.Value = CrownsInteger
	rank.Value = DecodedLvlString	
end			
	
return module
--Local function module
local module = {}
local DataStoreService = game:GetService("DataStoreService")
local LvlDS = DataStoreService:GetDataStore("LvlStats")
local CrownDS = DataStoreService:GetDataStore("Crowns")
local GemsDS = DataStoreService:GetDataStore("Gems")

function module.SetLevelInteger(plr,lvl)
	local success, err = pcall(function()
		local Data = lvl
		LvlDS:SetAsync(plr.UserId,Data)
		return Data
	end)
	if err then warn("DATA FAIL: "..err)end		 
end
		
function module.GetLevelInteger(plr)
	local success, err = pcall(function()
		local data = LvlDS:GetAsync(plr.UserId)
	    return data
	end)
	if err then warn("DATA FAIL: "..err)end
end		

function module.Leaderstats(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"
	
	local crowns = Instance.new("IntValue")
	crowns.Name = "Crowns"
	crowns.Parent = leaderstats
	
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Parent = leaderstats	
	
	local rank = Instance.new("StringValue")
	rank.Name = "Rank"
	rank.Parent = plr 
end

function module.Decode(LvlData)
	if LvlData == 0 then 
		return "Noob"
	elseif LvlData == 1 then
		return "Learner"
	elseif LvlData == 2 then
		return "Apprentice"
	elseif LvlData == 3 then
		return "Swordsmen"
	elseif LvlData == 4 then
		return "Novice"
	elseif LvlData == 5 then
		return "Expert"
	elseif LvlData == 6 then
		return "Master"
	elseif LvlData == 7 then
		return "Ruler"
	elseif LvlData == 8 then
		return "Legendary"
	elseif LvlData == 9 then
		return "God"
	end
end
	
function module.CodeUp(LvlData)
if LvlData == "Noob"then
return 0 
elseif LvlData == "Learner"then
return 1
elseif LvlData == "Apprentice"then
return 2
elseif LvlData == "Swordsmen"then
return 3
elseif LvlData == "Novice"then
return 4
elseif LvlData == "Expert"then
return 5
elseif LvlData == "Master"then
return 6
elseif LvlData == "Ruler"then
return 7
elseif LvlData == "Legendary"then
return 8 
elseif LvlData == "God"then
return 9
end
end 

function module.GetCrowns(plr,crownsObj)	
	local success, err = pcall(function()
		local Data = CrownDS:GetAsync(plr.UserId)
	    if Data then return Data
	end) 
	if err then warn("DATA FAIL:"..err)end
end

function module.SetCrowns(plr,crowns)
	local success, err = pcall(function()
		local Data = crowns
	    CrownDS:SetAsync(plr.UserId, Data)
	end)	
	if err then warn("DATA FAIL:"..err)end	
end

function module.GetGems(plr)
	local success, err = pcall(function()
		local Data = GemsDS:GetAsync(plr.UserId)
	if Data then return Data
	end)		
	if err then warn("DATA FAIL:"..err)end
end

local function SetGems(plr,gems)
	local success, err = pcall(function()
	    GemsDS:SetAsync(plr.UserId, Gems)
	end)
	if err then warn("DATA FAIL:"..err)end
end

return module

datamodulev2.rbxm (2.3 KB)