Script Not Running at All?

For my game, I’ve used the template data-saving script from ROBLOX’s dev thing. It works fine in other games I’ve made, but for whatever reason it simply won’t run now. It doesn’t throw any errors, however. It’s just that literally nothing happens.

-- Setup table that we will return to scripts that require the ModuleScript.
local PlayerStatManager = {}
 
-- Create variable for the DataStore.
local DataStoreService = game:GetService('DataStoreService')
local RS = game:GetService('ReplicatedStorage')
local playerData = DataStoreService:GetDataStore('BETA1')

--StarterData for new players

local StarterData = {Points=500}
 
 
-- Create variable to configure how often the game autosaves the player data.
local AUTOSAVE_INTERVAL = 60
 
-- Number of times we can retry accessing a DataStore before we give up and create
-- an error.
local DATASTORE_RETRIES = 3
 
-- Table to hold all of the player information for the current session.
local sessionData = {}

local function IsInTable(Table, Item)
	local In = false
	for i,v in pairs(Table) do
		if v == Item then
			In = true
		end
	end
	return In
end

local function IsInDict(Table, Item)
	local In = false
	for i,v in pairs(Table) do
		if i == Item then
			In = true
		end
	end
	return In
end

local function MakeTable(Table)
	local NewTable = {}
	for i,v in pairs(Table) do
		NewTable[i]=v
	end
	return NewTable 
end
 
-- Function the other scripts in our game can call to change a player's stats. This
-- function is stored in the returned table so external scripts can use it.
function PlayerStatManager:ChangeStat(player, statName, changeValue)
	sessionData[player][statName] = sessionData[player][statName] + changeValue
	print("Just changed the "..statName.." of: "..player.Name)
end

function PlayerStatManager:SetStat(player, statName, changeValue)
	sessionData[player][statName] = changeValue
	print("Just set the "..statName.." of: "..player.Name)
end

function PlayerStatManager:LoadStat(player, statName)
	print(sessionData[player][statName])
	return sessionData[player][statName]
end

function PlayerStatManager:AddTable(player, statName, addition)
	table.insert(sessionData[player][statName], addition)
	print("Added "..addition.. " to "..player.Name)
end

function PlayerStatManager:LoadTable(player, statName, number)
	return sessionData[player][statName][number]
end

function PlayerStatManager:SetTable(player, statName, number, addition)
	sessionData[player][statName][number] =  addition
	print("Just changed the table of: "..player.Name)
end

function PlayerStatManager:GetStats(player)
	return sessionData[player]
end

function PlayerStatManager:SetData(player, Data)
	sessionData[player] = Data
	print("Just changed the stats of: "..player.Name)
end
 
-- Function to retry the passed in function several times. If the passed in function
-- is unable to be run then this function returns false and creates an error.
local function dataStoreRetry(dataStoreFunction)
	local tries = 0	
	local success = true
	local data = nil
	repeat
		tries = tries + 1
		success, errorMessage = pcall(function() data = dataStoreFunction() end)
		print("Save successful", success, "errorMessage", errorMessage)
		if not success then wait(6) end
	until tries == DATASTORE_RETRIES or success
	if not success then
		error('Could not access DataStore! Warn players that their data might not get saved!')
	end
	return success, data
end
 
-- Function to retrieve player's data from the DataStore.
local function getPlayerData(player)
	return dataStoreRetry(function()
		return playerData:GetAsync(player.UserId)
	end)
end
 
-- Function to save player's data to the DataStore.
local function savePlayerData(player)
	RS.Events.DataSaved:FireClient(player)
	if sessionData[player] then
		return dataStoreRetry(function()
			return playerData:SetAsync(player.UserId, sessionData[player])
		end)
	end
end

function PlayerStatManager:SetStats(player, newStats)
	sessionData[player] = newStats
	savePlayerData(player)
	RS.Events.DataSaved:FireClient(player)
	print("Just set the stats of: "..player.Name)
end

-- Function to add player to the sessionData table. First check if the player has
-- data in the DataStore. If so, we'll use that. If not, we'll add the player to
-- the DataStore.
local function setupPlayerData(player)
	print('hi')
	local success, data = getPlayerData(player)
	if not success then
		-- Could not access DataStore, set session data for player to false.
		sessionData[player] = false
		print('sent')
	else
		if not data then
			-- DataStores are working, but no data for this player
			sessionData[player] = MakeTable(StarterData)
			print(player.Name.." has been given first-time stats")
			savePlayerData(player)
			RS.Events.DataGot:FireClient(player, sessionData[player])
		else
			-- DataStores are working and we got data for this player
			sessionData[player] = data
			print(player.Name.." has been given back their stats")
			for i,v in pairs(StarterData) do
				if not IsInDict(data, i) then
					sessionData[player][i] = v
				end
			end
			savePlayerData(player)
			RS.Events.DataGot:FireClient(player, sessionData[player])
		end
	end	
end
 
-- Function to run in the background to periodically save player's data.
local function autosave()
	while wait(AUTOSAVE_INTERVAL) do
		for player, data in pairs(sessionData) do
			savePlayerData(player)
		end
	end
end
 
-- Bind setupPlayerData to PlayerAdded to call it when player joins.
game.Players.PlayerAdded:connect(setupPlayerData)
 
-- Call savePlayerData on PlayerRemoving to save player data when they leave.
-- Also delete the player from the sessionData, as the player isn't in-game anymore.
game.Players.PlayerRemoving:connect(function(player)
	savePlayerData(player)
	sessionData[player] = nil
end)


-- Start running autosave function in the background.
spawn(autosave)
 
-- Return the PlayerStatManager table to external scripts can access it.
return PlayerStatManager

It’s a direct copy from another game that uses the same script and works perfectly fine. Why isn’t it working here? (In studio or in live servers)

2 Likes

Where is this script located in your explorer? What service is it a descendant of? Is it a local script or a server script?

1 Like

Sorry! I definitely should’ve included that in the original post. It’s a module script located in ServerStorage.

This looks like code from a module so it will not run until it is required

4 Likes

As @kingdom5 said, you want to require the module from a server script:

local DataModule = require(game.ServerStorage.DataModule)

Module scripts don’t run their code until they’re required.

2 Likes

ModuleScripts need to be required before they run. In other words, another script needs do “activate” it. You can do it using the global function require, as demostrated here:

local Module = require(game:GetService("ServerStorage") .ModuleNameHere)
3 Likes

Thank you all so much! feeling very embarrassed because i couldn’t figure it out but i never knew module scripts had to be required before they would run, and i never even thought to check!!

1 Like

Just to add; the global is game. Game does not work and will error.

Additionally, you should be using game:GetService(‘ServerStorage’) over game.ServerStorage!

1 Like

I saw that just as I posted it, but thanks for taking notice!