Need Help with DataStore for leaderboard Module Script

  1. What do you want to achieve? Keep it simple and clear!
    Finish making my LeaderBoard Module script

  2. What is the issue? Include screenshots / videos if possible!
    Data Won’t be saved when I leave and Join the game. I used the ModuleScript only to make the leaderboard. And get value when you click something. But it won’t save.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried changing the code to see if it would work but nothing it working.

Here is the Script :

local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")

local LeaderBoardModule = {}

local function Debug(Message)
	print(Message)
end

function LeaderBoardModule:New()
	Players.PlayerAdded:Connect(function(p)
		local LS = Instance.new("Folder")
		LS.Name = "leaderstats"
		LS.Parent = p
		
	end)
	self.__index = self 

	return setmetatable({},self)
end

function LeaderBoardModule:CreateValue(ValueType,ValueName,SaveLeaderboardData,DataStoreName,DataStoreKeyName,DebugOn)
	local Store = nil
	Players.PlayerAdded:Connect(function(p)
		local Folder = p:WaitForChild("leaderstats")
		local Value = Instance.new(ValueType)
		Value.Name = ValueName
		Value.Parent = Folder
		local Data = nil
		Value.Value = Data or 0
		
		if SaveLeaderboardData then
			local Sucess,ErrorMsg = pcall(function()
				wait()
				if Store then
					DataValue = Store:GetAsync(p.UserId,DataStoreKeyName)
				end
			end)
			
			if Sucess then
				Data = DataValue
				if DebugOn then
					Debug("Saved Data!")
				end
				
				
			else
				if DebugOn then
					error(ErrorMsg)
				end	
			end
		end
	end)
	
	Players.PlayerRemoving:Connect(function(p)
		if SaveLeaderboardData then
			local Data = DSS:GetDataStore(DataStoreName)
			local sucess,errorMsg = pcall(function()
				Data:SetAsync(p.UserId .. DataStoreKeyName,p:WaitForChild("leaderstats").ValueName.Value)
				
			end)
			
			if sucess then
				if DebugOn then
					Debug("Data was set!")
				
				end
			else
				if DebugOn then
					error(errorMsg)
				end	
				
			end
			Store = Data
		end
	end)
end

function LeaderBoardModule:Add(LeaderBoardValueName,Value,player) --This is for Ints
	wait()
	local Value = player.leaderstats:FindFirstChild(LeaderBoardValueName)
	Value.Value = Value.Value + Value
end




return LeaderBoardModule

Hey your problem is that you’re not using datastores properly in the module script, this topic should help you out: What is the difference between you using a datastore in a moduleScript and a normal script?