My player's levels reset to 1000

when one of my players killed something (they were over level 1000) and rejoined they would be back to 1000 or 1001, im not sure whats going on?

3 Likes

are you using DataStoreService to save their data and then load it back?

1 Like

im using profileservice to help with my datastore

profile service is an updated version of datastoreservice…

how is that gonna help with my problem

Show your scripts. We can’t just find out the problem by reading one sentence.

what do you want me to show? like what script

The saving script.

local DataStoreHandler = {
	Profiles = {}
}
DataStoreHandler.__index = DataStoreHandler

local TEMPLATE = {
	levelprogress = 0,
	levelmax = 100,
	--hp = 50,
	
	SpawnPoint = "forest",
	Race = "Hominarcs",
	RaceRolls = 0,
	
	["inventory"] = {
		[1] = "wood sword"
	},
	["equipped"] = {
		[1] = "wood sword"
	},
	["Enchantments"] = {},
	
	["Titles"] = {},
	["EquippedTitle"] = "None",
	
	levels = 1,
	gold = 0,
	
	DataTransfered = false,
	
	XPBoost = 0,
	GoldBoost = 0
}

local ProfileService = require(script.ProfileService)
local ReplicaService = require(game.ServerScriptService:WaitForChild("Scripts"):WaitForChild("ReplicaService"))
local PLAYERS = game:GetService("Players")

local PStore = ProfileService.GetProfileStore(
	"PlayerData",
	TEMPLATE
)

local Profiles = {}
local PLAYER_REPLICA = {}

local FOLDER_TEMPLATE = {
	["leaderstats"] = {
		"levels",
		"gold"
	},
	["PLAYER"] = {
		"levelprogress",
		"levelmax",
		"hp"
	}
}

local function TransferData(plr)
	local PROFILE = Profiles[plr]

	if PROFILE.Data.DataTransfered == true then return end
	
	local dataservice = game:GetService("DataStoreService")
	local GetData,GetData2,GetData3,GetData4,GetData5 = dataservice:GetDataStore("data1//"..plr.UserId):GetAsync(plr.UserId),dataservice:GetDataStore("weapon_data//"..plr.UserId):GetAsync(plr.UserId),dataservice:GetDataStore("data3//"..plr.UserId):GetAsync(plr.UserId),dataservice:GetDataStore("spawnpoint_data1//"..plr.UserId),dataservice:GetDataStore("equipped//"..plr.UserId):GetAsync(plr.UserId)
	
	local DATA = {
		["inventory"] = {},
		["equipped"] = {}
	}
	
	if GetData then
		for i,v in pairs(GetData) do
			DATA[i] = v
		end
	end
	
	if GetData2 then
		for i,v in pairs(GetData2) do
			table.insert(DATA.inventory,v)
		end
	end
	
	if GetData3 then
		for i,v in pairs(GetData3) do
			DATA[i] = v
		end
	end
	
	if GetData4 then
		DATA["SpawnPoint"] = GetData4:GetAsync(plr.UserId)
	end
	
	if GetData5 then
		for i,v in pairs(GetData5) do
			table.insert(DATA.equipped,v)
		end
	end
	
	for i,v in pairs(DATA) do
		PROFILE.Data[i] = v
	end
	
	PROFILE.Data.DataTransfered = true
end

local function SetUpFolders(plr)
	local profile = Profiles[plr]
	for name,values in pairs(FOLDER_TEMPLATE) do
		if name ~= "PLAYER" then
			local folder = Instance.new("Folder",plr)
			folder.Name = name
			for _,value in pairs(values) do
				if profile.Data[value] then
					if typeof(profile.Data[value]) == "number" then
						local v = Instance.new("NumberValue",folder)
						v.Value = profile.Data[value]
						v.Name = value
					elseif typeof(profile.Data[value]) == "string" then
						local v = Instance.new("StringValue",folder)
						v.Value = profile.Data[value]
						v.Name = value		
					end
				end
			end
			
		else
			
			for _,value in pairs(values) do
				if profile.Data[value] then
					if typeof(profile.Data[value]) == "number" then
						local v = Instance.new("NumberValue",plr)
						v.Value = profile.Data[value]
						v.Name = value
					elseif typeof(profile.Data[value]) == "string" then
						local v = Instance.new("StringValue",plr)
						v.Value = profile.Data[value]
						v.Name = value		
					end
				end
			end
			
		end

	end
end

local function playerAdded(plr)
	local profile = PStore:LoadProfileAsync("Data//"..plr.UserId) 

	if profile then
		profile:AddUserId(plr.UserId)
		profile:Reconcile()
		
		profile:ListenToRelease(function()
			print("releasing")
			Profiles[plr] = nil
			PLAYER_REPLICA[plr]:Destroy()
			DataStoreHandler.Profiles[plr] = nil
			
			plr:Kick()
		end)
		
		if not plr:IsDescendantOf(PLAYERS) then
			profile:Release()
		else
			Profiles[plr] = profile
			DataStoreHandler.Profiles[plr] = profile
			
			SetUpFolders(plr)
			TransferData(plr)
			
			print(plr.Name.." level is "..profile.Data.levels)
			
			task.spawn(function()
				for i,v in pairs(profile.Data) do
					if not TEMPLATE[i] then
						profile.Data[i] = nil
					end
				end
			end)
			
			PLAYER_REPLICA[plr] = ReplicaService.NewReplica({
				ClassToken = ReplicaService.NewClassToken("PlrReplica//"..plr.UserId),
				Data = profile.Data,
				Replication = plr
			})
		end
	else
		plr:Kick("Error loading data. Please try again later.")
	end
end

local function BindToClose()
	for _,plr in pairs(game.Players:GetPlayers()) do
		Profiles[plr]:Save()
		Profiles[plr]:Release()
	end
end

function DataStoreHandler:Init(player)
	playerAdded(player)
	
	game.Players.PlayerRemoving:Connect(function(plr)
		if Profiles[plr] then
			Profiles[plr]:Save()
			Profiles[plr]:Release()
			for i,v in pairs(workspace.Enemies:GetChildren()) do
				local attacker = v:FindFirstChild("attacker")
				if attacker and attacker.Value == plr.Name then
					attacker.Value = ""
				end
			end
		end
	end)
end

local function GetProfile(plr)
	local profile = PLAYER_REPLICA[plr]

	while not profile do
		profile = PLAYER_REPLICA[plr]

		if profile then
			break
		end

		task.wait()
	end
	assert(Profiles[plr],string.format("profile doesnt exist for %s",plr.UserId))
	assert(PLAYER_REPLICA[plr],string.format("replica doesnt exist for %s",plr.UserId))
	return Profiles[plr]
end


function DataStoreHandler:Get(plr,key)
	local profile = GetProfile(plr)
	assert(profile.Data[key],string.format("data doesnt exist for %s",key))
	return profile.Data[key]
end

function DataStoreHandler:Set(plr,key,value)
	local profile = GetProfile(plr)
	assert(profile.Data[key],string.format("data doesnt exist for %s",key))
	assert(type(profile.Data[key]) == type(value))
	
	profile.Data[key] = value
	PLAYER_REPLICA[plr]:SetValue({key},value)
	
	print(plr.Name.." level is "..profile.Data.levels)
	
	for _,v in pairs(plr:GetDescendants()) do
		if v.Name == key then
			for i,v2 in pairs(FOLDER_TEMPLATE) do
				if i == v.Parent.Name then
					v.Value = value
				end
			end
			if v.Parent == plr then
				v.Value = value
			end
		end
	end
	
end

function DataStoreHandler:Update(plr,key,callback)
	local profile = GetProfile(plr)
	
	local olddata = self:Get(plr,key)
	local newdata = callback(olddata)
	
	self:Set(plr,key,newdata)
end

game:BindToClose(BindToClose)

return DataStoreHandler

Datastores look fine to me, better than most ive seen. is this issue consistent and easy to reproduce?

the issue seems consistent, i went so insane that i think its my script and remaking the script that manages levels

i remade the script, didn’t fix the issue at all, do not know what to do at this point