Help me with my save and load data

save and load data script doesen’t work with the error ServerScriptService.PlayerData.Data:58: attempt to index nil with Instance

  1. What do you want to achieve? Keep it simple and clear!
    i wanted to save and load player data however player joins/leaves the game

  2. What is the issue? Include screenshots / videos if possible!
    my script isn’t working. it show the error ServerScriptService.PlayerData.Data:58: attempt to index nil with Instance

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes, i have tried searching about this particular topic with the same tutorial, but i come out empty-handed.

4.Additional information
my script was in serverscriptservice
i followed the following tutorial: The Best and Easiest Way To Save and Load Data
i used ProfileService
here is my 3 scripts:
the Data serverscript:

local players = game:GetService("Players")
local serverscriptservice = game:GetService("ServerScriptService")

local template = require(serverscriptservice.PlayerData.Template)
local manager = require(serverscriptservice.PlayerData.Manager)
local profileservice = require(serverscriptservice.OtherScripts.ProfileService)

local profilestore = profileservice.GetProfileStore("production", template)
local KICK_MESSAGE = "Sorry, we're having trouble gathering your data, please try again later"

local function createleaderstats(player: Player)
	local profile = manager.profiles[player]
	if not profile then return end
	
	local folder1 = Instance.new("Folder", player)
	folder1.Name = "leaderstats"

	local folder2 = Instance.new("Folder", player)
	folder2.Name = "stats"

	local money1 = Instance.new("NumberValue", folder1)
	money1.Name = "Coins"
	money1.Value = profile.Data.Coins

	local money2 = Instance.new("NumberValue", folder2)
	money2.Name = "Meteorites"
	money2.Value = profile.Data.Meteorites
end

local function loadprofile(player: Player)
	local profile = profilestore:LoadProfileAsync("Player_"..player.UserId)
	if not profile then
		player:Kick(KICK_MESSAGE)
		return
	end
	
	profile:AddUserId(player.UserId)
	profile:Reconcile()
	profile:listentorelease(function()
		manager.profiles[player] = nil
		player:kick(KICK_MESSAGE)
	end)
	
	if player:IsDescendantOf(players) == true then
		manager.profiles[player] = profile
		createleaderstats(player)
	else
		profile.Release()
	end
end

for _, player in players:GetPlayers() do
	task.spawn(loadprofile, player)
end

players.PlayerAdded:Connect(loadprofile)
players.PlayerRemoving:Connect(function(player)
	local profile = manager.Profiles[player]
	if profile then
		profile:Release()
	end
end)

the manager modulescript:

local manager = {}

manager.profiles = {}

return manager

the template modulescript:

local template = {
	Coins = 200,
	Meteorites = 0

}

return template

all of that is probably all of the information that i can give, all helps will be appriciated!
thanks! :grinning:

if i didn’t respond within a hour, i was at school or i was sleeping.

for save, you must use the DataStore, exactly SetAsync and GetAsync

Soo that tutorial is not working nymore?

Hm…
Examples:
Saving:

local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("Data")
local success, errorMessage = pcall(function()
	experienceStore:SetAsync("User", 1)
end)
if not success then
	print(errorMessage)
end

Loading:

local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("Data")
local success, currentExperience = pcall(function()
	return experienceStore:GetAsync("User")
end)
if success then
	print(currentExperience)
end

But you must enable “Studio Access to API services” in game settings>security
And i dont know what tutorial do you mean

alright, thanks for letting me know

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