ProfileService DataStore did not work for me

I was following a Tutorial on how to use ProfileService DataStore made by @okeanskiy
Video Title on YT: Intro to ProfileService DataStore Module: Basic Usage (Roblox Studio)

Error I got from output is Module code did not return exactly one value and Requested module experienced an error while loading

I don’t have any solution for this because I don’t know the problem. Maybe, I’m just blind and did not see any typing error or missed some codes from the tutorial.

DataManager (modulescript)

local Players = game:GetService("Players")
local ProfileService = require(game.ReplicatedStorage.ProfileService)

local ProfileStore = ProfileService.GetProfileStore(
	"Player",
	{
		money = 0;
		lastUserdMoneyPartTime = 0;
	}
)

local Profiles = {}

local function onPlayerAdded(player)
	local profile = ProfileStore:LoadProfileAsync(
		"Player_".. player.UserId,
		"ForceLoad"
	)
	
	if profile then
		profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick()
		end)
		
		if player:IsDescendantOf(Players) then
			Profiles[player] = profile
		else
			profile:Release()
		end
	else
		player:Kick()
	end
end

local function onPlayerRemoving(player)
	local profile = Profiles[player]
	if profile then
		profile:Release()
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

local DataManager = {}

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

Just in case, I put this money script too if the error caused from here

Money (script)

local DataManager = require(game.ReplicatedStorage.DataManager)

local part = workspace.Money

local function onTouched(touchedPart)
	local player = game.Players:GetPlayerFromCharacter(touchedPart.Parent)
	
	if player then 
		local data = DataManager:Get(player)
		
		if data then
			local currentTime = os.time()
			local timeSinceLastUsedMoneyPart = currentTime - data.lastUsedMoneyPartTime
			local waitTime = 10*60
			
			if timeSinceLastUsedMoneyPart > waitTime then
				data.lastUsedMoneyParTime = currentTime
				data.money += 500
				
				print(player.Name, "now has money:", data.money)
			else
				print(player.Name, "must wait seconds before use: ", waitTime - timeSinceLastUsedMoneyPart)
			end
		else
			print(player.Name, "does not have data profile loaded")
		end
	end
end

part.Touched:Connect(onTouched)

Thank you in advanced :smiley:

1 Like

Module code did not return exactly one value
This means your module script is not returning a table containing the actual module functions. Just add this line at the end:
return DataManager

Requested module experienced an error while loading
This error is thrown when a module you request had an error upon initialization and is unavailable, this error should also appear in output but you did not include it. Look for any other red text that gives more detailed error info and stacktrace.

2 Likes

Great! The error is gone but now there is another error ServerScriptService.Script:13: attempt to perform arithmetic (sub) on number and nil on the Money Script at local timeSinceLastUsedMoneyPart = currentTime - data.lastUsedMoneyPartTime

You misspelt the data name in the original table, but I recommend keeping this line in as a redundancy check.

That means data.lastUsedMoneyPartTime is nil, or has no value. Lua cannot perform a mathematical operation of nil values and freaks out. Add a check that ensures data.lastUsedMoneyPartTime is not nil like this:

if not data.lastUsedMoneyPartTime then
 data.lastUsedMoneyPartTime = 0
end

I also noticed that you are modifying the values within data after getting them in the money script. This is fine, but you are not sending this data back to the DataManager script so it is not saving those values.

1 Like

ProfileService theoretically should have set lastUserdMoneyPartTime to 0 as it is defined when GetProfileStore is invoked.

1 Like

Yes, you are correct.

@MangoRanger it also appears you misspelt the data name in the DataManager, I assume it should be “lastUsedMoneyPartTime” not “lastUserdMoneyPartTime” on line 8.

2 Likes

I corrected the misspelt but it still have the same error

Oh i think i did miss some codes from the tutorial