Why is my module script running more than once?

i have a datastore module

local data = {}

local DatastoreService = game:GetService("DataStoreService")
local PlayerData = DatastoreService:GetDataStore("PlayerData")

--local setStats = require(script.setStats)
local defaultData = require(script.defaultData)
local dataTables = {}
local dataTableChanged = game:GetService("ServerStorage").dataTableChanged

print("hello?")

function data.start(player: Player)
	local userData = PlayerData:GetAsync(player.UserId)
	if not userData then
		userData = defaultData
	else
		syncDataWithDefaults(userData, defaultData)
	end
	dataTables[player] = userData
	print(dataTables[player])
	return dataTables[player]
end

ok and in a script in serverscriptservice

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		CharacterInit:init(Character)
	end)
	
	
	dataModule.start(Player)

and it prints “hello?”

and then in a stat script

local Ego = {}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local dataModule = require(ServerStorage.Modules.Player.data)

function Ego.IncrementStat(player: Player, stat: string, amount, color)
	local statx = dataModule.getData(player)
	print(statx)
	if amount >= 0 then
		ReplicatedStorage.events.reward:FireClient(player, stat, amount, color, true)
	elseif amount < 0 then
		ReplicatedStorage.events.reward:FireClient(player, stat, amount, color, false)
	end
	dataModule.incrementData(player, stat, amount)
end

it prints “hello?” again AND it says nil because theres nothing in the dataTables table
??? what is happening why is it running twice

hello! sorry for writing this for not related to this issue but maybe this can help you its a module i made pDataService can help you a lot with game saving progress and much more so you dont have to waste time making your data system, simply write your own interface easly with this theres even docs site with even pre-made module that can show use of this system.

yall i figured out that for some reason this happens when you use the command line, runs in a different enviroment i guess

That would be correct. The server, client and command line each run on a different VM - copies of the ModuleScript are not shared between each of these environments, which means that a different object (and therefore a rerunning of your top-level code) is performed with each environment’s require. You should set up some testing tools in light of this.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.