Problem with module script running functions

I have been creating a ProfileService modulescript that deals with the players data. In my testing place, the code works fine and the modulescript is able to detect when a player joins, and runs the functions. In the other place, it doesn’t run any functions when the player joins. The scripts in both places are identical, and are both under ServerScriptService.

1 Like

We need a bit more information than “it works here but not there”. Sharing the code and general context of the module as well as the situation is important if you want it to be solved.

1 Like
local PFS = require(game.ReplicatedStorage.ProfileService)

local profileTemplate = {
	cash = 0,
	houseInfo = {
		houseFurniture = {},
		houseBlueprint = "small"
	},
	inventoryInfo = {
		furnitureInventory = {
			"Chair",
			"Desk"
		},
		petInventory = {
			
		}
	},
}

local PFSstore = PFS.GetProfileStore(
	"Player",
	profileTemplate
)
local Profiles = {}
local DataManager = {}

function RoundNum(num)
	if num ~= nil then
		return math.floor((num*10)/10)
	end
end

function CreateData(player, playerProfile)	
	player:SetAttribute("Cash", playerProfile.Data.cash)
	
	--make their inventory
	--[[local inventory = Instance.new("Folder")
	inventory.Parent = player
	inventory.Name = "Inventory"

	--make furniture inventory
	local furniture = Instance.new("Folder")
	furniture.Parent = inventory
	furniture.Name = "furniture"

	for i,v in pairs(playerProfile.Data.inventoryInfo.furnitureInventory) do
		furniture:SetAttribute(v, v)
	end--]]

end

function CheckMissingData(playerProfile)
	if playerProfile.Data.cash == nil then
		playerProfile.Data["cash"] = 0
		print(playerProfile.Data)
	--	table.insert(playerProfile.Data, #playerProfile.Data + 1, Cash)
	end
end

function LoadHouse(player, playerProfile)
	print(playerProfile.Data)
	--load house blueprint
	local houseBlueprint = playerProfile.Data.houseInfo["houseBlueprint"]
	local serverHouse = game.ServerStorage.houseBlueprints:FindFirstChild(houseBlueprint)
	if serverHouse then
		local playerHouse = serverHouse:Clone()
		playerHouse.Name = player.Name
		playerHouse.Parent = game.Workspace.Houses
	end


	for i,v in pairs(playerProfile.Data.houseInfo.houseFurniture) do
		print(i,v)
		local furnitureType = v[1]
		local furniture = game.ReplicatedStorage.placementSystem.models:FindFirstChild(furnitureType)
		if furniture then
			local furniture = game.ReplicatedStorage.placementSystem.models:FindFirstChild(furnitureType):clone()
			furniture.Parent = game.Workspace.Houses:FindFirstChild(player.Name).furniture
			local YOrientation = (tonumber(v[5]))
			furniture:SetPrimaryPartCFrame(CFrame.Angles(0, math.rad(YOrientation), 0))
			furniture:MoveTo(game.Workspace.Houses:FindFirstChild(player.Name).Position + Vector3.new(RoundNum(v[2]), RoundNum(v[3]), RoundNum(v[4])))
			if furniture:FindFirstChild("hitbox") then
				furniture.hitbox.Transparency = 1
			end
			for i,v in pairs(furniture:GetChildren()) do
				if v:IsA("BasePart") and v.Name ~= "hitbox" then
					v.CanCollide = true
				end
			end
		end
	end
end

function SaveHouse(player, playerProfile)
	playerProfile.Data.houseInfo.houseFurniture = {}
	for i,v in pairs(game.Workspace.Houses:FindFirstChild(player.Name).furniture:GetChildren()) do
		if v:IsA("Model") then
			print("ok")
			local xPos = RoundNum(v.hitbox.Position.X - game.Workspace.Houses:FindFirstChild(player.Name).Position.X)
			local yPos = RoundNum(v.hitbox.Position.Y - game.Workspace.Houses:FindFirstChild(player.Name).Position.Y)
			local zPos = RoundNum(v.hitbox.Position.Z - game.Workspace.Houses:FindFirstChild(player.Name).Position.Z)
			local orientation = tostring(v.hitbox.Orientation.Y)
			local furnitureName = v.Name

			local furnitureTable = {furnitureName, xPos, yPos, zPos, orientation}
			table.insert(playerProfile.Data.houseInfo.houseFurniture, furnitureTable)
		end
	end
	local houseBlueprintIs = game.Workspace.Houses:FindFirstChild(player.Name):GetAttribute("Blueprint")
	playerProfile.Data.houseInfo["houseBlueprint"] = houseBlueprintIs
end

function SaveInventory(player, playerProfile)
	local playerFurnitureInventory = player.Inventory.furniture
	playerProfile.Data.inventoryInfo.furnitureInventory = {}
	for i,v in pairs(playerFurnitureInventory:GetAttributes()) do
		playerProfile.Data.inventoryInfo.furnitureInventory[#playerProfile.Data.inventoryInfo.furnitureInventory + 1] = v
	end
end

game.ReplicatedStorage.AddInventoryItem.OnServerEvent:Connect(function(plr, itemType, item, reason)
	local playerProfile = Profiles[plr]
	if reason == "add" then
		if itemType == "furniture" then
			local furnitureModel = game.ReplicatedStorage.placementSystem.models:FindFirstChild(item)
			if furnitureModel then
				table.insert(playerProfile.Data.inventoryInfo.furnitureInventory, item)
			end
		end		
	end
	if reason == "remove" then
		if itemType == "furniture" then
			local furnitureModel = game.ReplicatedStorage.placementSystem.models:FindFirstChild(item)
			if furnitureModel then
				local tablefind = table.find(playerProfile.Data.inventoryInfo.furnitureInventory, item)
				if tablefind ~= nil then
					table.remove(playerProfile.Data.inventoryInfo.furnitureInventory, tablefind)
				end
			end
		end		
	end
end)

function PlayerAdded(player)
	print("PLAYER JOIN")
	local playerProfile = PFSstore:LoadProfileAsync("Player_"..player.UserId, "ForceLoad")

	if playerProfile then
		playerProfile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick("Your data failed to load, please rejoin")
		end)

		if player:IsDescendantOf(game.Players) then
			Profiles[player] = playerProfile
			CheckMissingData(playerProfile)
			CreateData(player, playerProfile)
			--LoadHouse(player, playerProfile)
		else
			playerProfile:Release()
		end
	else
		player:Kick("Your data failed to load, please rejoin")
	end
end

function PlayerRemoving(player)
	local playerProfile = Profiles[player]
	if playerProfile then
		--SaveHouse(player, playerProfile)
		--SaveInventory(player, playerProfile)
		playerProfile:Release()
	end
end


game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(PlayerRemoving)

local DataManager = {}

function DataManager:Get(player)
	local profile = Profiles[player]
	if profile then
		return profile
	end
end

return DataManager

This modulescript uses the ProfileService module in ReplicatedStorage to load the players data, such as their house, inventory, and cash, once the player joins. On my testing game, which has only required things on it, the modulescript runs normally and loads the players data. When I copy the script to another game for it to be used, the modulescript does not even run a print statement for when a player joins. I don’t know if this has anything to do with it, but the game I’m transferring it to has team create enabled.

It could be that your modulescript is not called or its called too late for the event to be registered. Have you tried checking for that?

1 Like

This is the problem. In the test game, I had a script that was calling on the module, but I had not implemented it into the other game yet.