Datastore is not stable

  1. I want to achieve that my datastore is working correctly without any data leaks or kicking for no reason.

  2. My issue is that sometimes the player gets kicked in middle of playing the game with a data error.

  3. I have no idea what the problem is because I’m not really that of a specific when it comes to creating datastores.

This is the Datastore’s Code

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


local manager = require(script.Parent:WaitForChild("Manager"))
local Template = require(script.Parent:WaitForChild("Template"))
local ProfileService = require(script.Parent:WaitForChild("ProfileService"))


local ProfileStore = ProfileService.GetProfileStore("Gamu_Data2",Template)

local function leaderstats(player : Player)
	local profile = manager.Profiles[player]
	if not profile then return end
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local DataFolder = Instance.new("Folder")
	DataFolder.Parent = player
	DataFolder.Name = "DataFolder"
	
	local Bounty = Instance.new("NumberValue")
	Bounty.Name = "Bounty"
	Bounty.Parent = leaderstats
	Bounty.Value = profile.Data.Bounty
	
	local Beli = Instance.new("NumberValue")
	Beli.Name = "Beli"
	Beli.Parent = DataFolder
	Beli.Value = profile.Data.Beli

	local Strength = Instance.new("NumberValue")
	Strength.Name = "StrengthLevel"
	Strength.Parent = DataFolder
	Strength.Value = profile.Data.Strength
	
	local Defense = Instance.new("NumberValue")
	Defense.Name = "DefenseLevel"
	Defense.Parent = DataFolder
	Defense.Value = profile.Data.Defense
	
	local Sword = Instance.new("NumberValue")
	Sword.Name = "SwordLevel"
	Sword.Parent = DataFolder
	Sword.Value = profile.Data.Sword
	
	local Gun = Instance.new("NumberValue")
	Gun.Name = "GunLevel"
	Gun.Parent = DataFolder
	Gun.Value = profile.Data.Gun

	local Haki = Instance.new("NumberValue")
	Haki.Name = "HakiLevel"
	Haki.Parent = DataFolder
	Haki.Value = profile.Data.Haki
	
	
	--STATS--
	local expfolder = Instance.new("Folder")
	expfolder.Parent = DataFolder	
	expfolder.Name = "EXPFolder"
	
	local HakiEXP = Instance.new("NumberValue")
	HakiEXP.Name = "HakiEXP"
	HakiEXP.Parent = expfolder
	HakiEXP.Value = profile.Data.HakiEXP
	local StrengthEXP = Instance.new("NumberValue")
	StrengthEXP.Name = "StrengthEXP"
	StrengthEXP.Parent = expfolder
	StrengthEXP.Value = profile.Data.StrengthEXP
	local DefenseEXP = Instance.new("NumberValue")
	DefenseEXP.Name = "DefenseEXP"
	DefenseEXP.Parent = expfolder
	DefenseEXP.Value = profile.Data.DefenseEXP
	local SwordEXP = Instance.new("NumberValue")
	SwordEXP.Name = "SwordEXP"
	SwordEXP.Parent = expfolder
	SwordEXP.Value = profile.Data.SwordEXP
	local GunEXP = Instance.new("NumberValue")
	GunEXP.Name = "GunEXP"
	GunEXP.Parent = expfolder
	GunEXP.Value = profile.Data.GunEXP
	
	
	--STATS--
	
	
	--MAX STATS--
	local HakiMaxEXP = Instance.new("NumberValue")
	HakiMaxEXP.Name = "HakiMaxEXP"
	HakiMaxEXP.Parent = expfolder
	HakiMaxEXP.Value = profile.Data.HakiMaxEXP
	local StrengthMaxEXP = Instance.new("NumberValue")
	StrengthMaxEXP.Name = "StrengthMaxEXP"
	StrengthMaxEXP.Parent = expfolder
	StrengthMaxEXP.Value = profile.Data.StrengthMaxEXP
	local DefenseMaxEXP = Instance.new("NumberValue")
	DefenseMaxEXP.Name = "DefenseMaxEXP"
	DefenseMaxEXP.Parent = expfolder
	DefenseMaxEXP.Value = profile.Data.DefenseMaxEXP
	local SwordMaxEXP = Instance.new("NumberValue")
	SwordMaxEXP.Name = "SwordMaxEXP"
	SwordMaxEXP.Parent = expfolder
	SwordMaxEXP.Value = profile.Data.SwordMaxEXP
	local GunMaxEXP = Instance.new("NumberValue")
	GunMaxEXP.Name = "GunMaxEXP"
	GunMaxEXP.Parent = expfolder
	GunMaxEXP.Value = profile.Data.GunMaxEXP
	--MAX STATS--
	
	--DevilFruit--
	local DevilFruit = Instance.new("NumberValue")
	DevilFruit.Name = "DevilFruit"
	DevilFruit.Parent = DataFolder
	DevilFruit.Value = profile.Data.DevilFruit
	--DevilFruit--
end



local function lvlupeffect(chr,color)
	local sound = script:WaitForChild("LVLSFX"):Clone()
	sound.Parent = chr:FindFirstChild("HumanoidRootPart")
	sound:Play()
	local effect = game:GetService("ReplicatedStorage"):FindFirstChild("ParticleFolder"):FindFirstChild("LVLUP"):WaitForChild("Attachment"):Clone()
	game:GetService("Debris"):AddItem(sound,3)
	effect.Parent = chr:FindFirstChild("HumanoidRootPart")
	effect.p1.Color = color
	effect.p2.Color = color
	effect.p1:Emit(20)
	effect.p2:Emit(20)
end
local function PlayerAdded(player : Player)
	
	local Profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
	
	if Profile == nil then
		player:Kick("Data Failed To Load(REJOIN!)")
		return
	end
	Profile:AddUserId(player.UserId)
	Profile:Reconcile()
	
	Profile:ListenToRelease(function()
		manager.Profiles[player] = nil
		player:Kick("Data Failed To Load(REJOIN!)")
	end)
	
	if player:IsDescendantOf(Players) == true then
		manager.Profiles[player] = Profile
		leaderstats(player)
	else
		Profile:Release()
	end
	local datafolder = player:WaitForChild("DataFolder")
	local EXPFolder = datafolder:WaitForChild("EXPFolder")
	
	
	local StrengthLevel = datafolder:WaitForChild("StrengthLevel")
	local StrengthEXP = EXPFolder:WaitForChild("StrengthEXP")
	local StrengthMaxEXP = EXPFolder:WaitForChild("StrengthMaxEXP")
	StrengthEXP:GetPropertyChangedSignal("Value"):Connect(function()
		if StrengthEXP.Value >= StrengthMaxEXP.Value then
			local sum = StrengthEXP.Value-StrengthMaxEXP.Value
			print(StrengthEXP.Value.." "..StrengthMaxEXP.Value)
			Profile.Data.StrengthEXP = sum
			Profile.Data.StrengthMaxEXP *= 1.175
            Profile.Data.Strength += 1
			StrengthLevel.Value += 1
			StrengthEXP.Value = StrengthEXP.Value-StrengthMaxEXP.Value
			StrengthMaxEXP.Value += 10
			local chr = player.Character
			if chr ~= nil then
				lvlupeffect(chr,ColorSequence.new(Color3.fromRGB(255, 0, 0)))
			end
		end
	end)
	
	-- DEFENSE
	local DefenseLevel = datafolder:WaitForChild("DefenseLevel")
	local DefenseEXP = EXPFolder:WaitForChild("DefenseEXP")
	local DefenseMaxEXP = EXPFolder:WaitForChild("DefenseMaxEXP")
	DefenseEXP:GetPropertyChangedSignal("Value"):Connect(function()
		if DefenseEXP.Value >= DefenseMaxEXP.Value then
			local sum = StrengthEXP.Value-StrengthMaxEXP.Value
			Profile.Data.DefenseEXP = sum
			Profile.Data.DefenseMaxEXP *= 1.175
			Profile.Data.Defense += 1
			DefenseLevel.Value += 1
			DefenseEXP.Value = DefenseEXP.Value-DefenseMaxEXP.Value
			DefenseMaxEXP.Value += 10
			local chr = player.Character
			if chr ~= nil then
				lvlupeffect(chr,ColorSequence.new(Color3.fromRGB(0, 255, 0)))
			end
		end
	end)
	
	
end



for _,player in Players:GetPlayers() do
	task.spawn(PlayerAdded,player)
end


Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player : Player)
	local Profile = manager.Profiles[player]
	
	if not Profile then return end
	
	Profile:Release()
end)
local function Addstats(player,stat,amount)
	local Profile = manager.Profiles[player]
	if not Profile then return end
	print("Added "..amount.."To".."Stat is now "..stat.Value)
	Profile.Data[stat.Name] += amount
	stat.Value = Profile.Data[stat.Name]
end

game:GetService("ReplicatedStorage"):WaitForChild("AddStatus").Event:Connect(Addstats)

Please i would be so happy if someone could find the problem for me.

1 Like

What is the “data error”?

minimum message length

Too much to read. But I dont see you setting async anywhere and use:
game:BindToClose(function()
task.wait(5) – - As this function should solve your players getting kicked and data not saving problem
end)

They are using ProfileService. It handles all of the getting and saving for you, but you have to set it up properly.

1 Like

Ah yeah sorry for the mistake I learnt about Profile Service too now. Didn’t know at the time.

Follow the video and itll work perfectly fine cuz am still not good enough to identify profile service problems