Requiring module from other script to another one

I am making a game and I’ve already implemented my data system using ProfileStore. I am often requiring my setup script (my script to make ProfileStore load and get data), and I’m getting tired of requiring each single time the setup script to obtain my data. I remembered that I recently saw a technique that allows a script to make another script script or multiple require a module of your own choice. That is perfect for my issue! Does anyone have any idea on how to achieve this goal?

(post deleted by author)

1 Like

What in the world? Shared table? Is that what ChatGPT came up with? You can’t just throw ChatGPT responses into someones topic. It is just incorrect.

1 Like

Yes, they are using chatgpt. I can’t blame them since there is no rule regarding AI for responses but this does block legitimate programmers responses.

2 Likes

Your response does not make any sence! You just provided variables which don’t require the other script, and instead itself only!

1 Like

My bad! I didn’t see “ProfileStore”, the script I provided is for profileservice so I suggest you watch a tutorial like this one:

Profileservice module

What do you mean by constantly “requiring” your module? You can setup a manager module and require it only once but you would still have to use module.LoadProfile(userId) (example), an alternative you can use is directly managing and loading data inside the main script but I dont recommend this. If you want to setup a manager script you can do something like this:

local ProfileService = require(script.Parent.ProfileService)
local ProfileTemplate = require(script.Parent.Template) -- your template script, you can just set this to a template table

local ProfileStore = ProfileService.GetProfileStore("Name", ProfileTemplate.DataTemplate)
local playerProfiles = {}

local PlayerData = {}

function PlayerData.GetProfile(player)
	if playerProfiles[player.UserId] then
		return playerProfiles[player.UserId]
	end

	local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)
	if profile then
		profile:Reconcile()
		profile:ListenToRelease(function()
			playerProfiles[player.UserId] = nil
			player:Kick("Data released.")
		end)
		if player:IsDescendantOf(game.Players) == true then
			playerProfiles[player.UserId] = profile
		else
			profile:Release()
		end
		return profile
	else
		player:Kick("Data failed to load.")
		return nil
	end
end

return PlayerData
3 Likes

This is the exact video I watched to make the ProfileStore Script!

I want a script that loops every decendant of the game, and if it is a ServerScript, a ClientScript or a ModuleScript, then the script would automaticaly recognise and require() the ProfileStore script and essentialy “autocomplete” it and not give out an error what does the ProfileStore mean. This will give the option to write

ProfileStore:GetData(Player)

and give me the PlayerData handed to the script without ever writing

local ProfileStore = require(game.ServerScriptService.Folder.Folder.Folder.Folder.ProfileStore)
1 Like

This is not a practical approach but if you insist on it you can use something like:

local function requireFunction()
	for _, obj in pairs(game.ServerScriptService:GetDescendants()) do
		if (obj:IsA("ModuleScript") or obj:IsA("Script")) and obj.Name == "ProfileStore" then
			return require(obj)
		end
	end
end
1 Like