I think theres something wrong with my datastore

My datastore is not okay. Sometimes I leave and rejoin then my inventory is gone, everything is not saved. This glitch has started more frequently recently. Please help me with this… Here’s the code

local dts = game:GetService("DataStoreService")
local plrdata = dts:GetDataStore("PlayerData")
local plrdatasorted1 = dts:GetOrderedDataStore("PlayerDataSorted1")
local plrdatasorted2 = dts:GetOrderedDataStore("PlayerDataSorted2")
local market = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)
	local IsOnMatch = Instance.new("BoolValue",player)
	IsOnMatch.Name = "IsOnMatch"
	local Coins = Instance.new("IntValue",player);
	Coins.Name = "Coins";
	Coins.Value = 999999
	local Kills = Instance.new("IntValue",player)
	Kills.Name = "Kills"
	local Level = Instance.new("IntValue",player)
	Level.Value = 1
	Level.Name = "Level"
	local ReqExp = Instance.new("IntValue",player)
	ReqExp.Name = "ReqExp"
	ReqExp.Value = 10
	local Exp = Instance.new("IntValue",player)
	Exp.Name = "Exp"
	local IsVIP = Instance.new("BoolValue",player)
	IsVIP.Name = "IsVIP"
	if market:UserOwnsGamePassAsync(player.UserId,703072321) then
		IsVIP.Value = true
	end
	local Slots = Instance.new("IntValue",player)
	Slots.Name = "Slots"
	Slots.Value = 5
	if IsVIP.Value then
		Slots.Value+=1
	end
	local Commands = Instance.new("Folder",player)
	Commands.Name = "Commands"
	local getdata = plrdata:GetAsync(player.UserId)
	if getdata then
	 Coins.Value = getdata[1]
	 ReqExp.Value = getdata[2]
	 Exp.Value = getdata[3]
	 for _,com in pairs(getdata[4]) do
		local newcom = Instance.new("BoolValue",Commands)
		newcom.Name = com.Name
		newcom.Value = com.Value
	 end
	end
	local getorderedata1 = plrdatasorted1:GetAsync(player.UserId)
	if getorderedata1 then
		Level.Value = getorderedata1
	end
	local getorderedata2 = plrdatasorted2:GetAsync(player.UserId)
	if getorderedata2 then
		Kills.Value = getorderedata2
	end
	if #Commands:GetChildren() == 0 then
		---creates default commands
		local haha1 = Instance.new("BoolValue",Commands)
		haha1.Name = "Purple Beam"
		haha1.Value = true
		local haha2 = Instance.new("BoolValue",Commands)
		haha2.Name = "Kitchen Gun"
		haha2.Value = true
		local haha3 = Instance.new("BoolValue",Commands)
		haha3.Name = "Speed Boost"
		haha3.Value = true
		local haha4 = Instance.new("BoolValue",Commands)
		haha4.Name = "Rocky Stomp"
		haha4.Value = true
		local haha5 = Instance.new("BoolValue",Commands)
		haha5.Name = "Heal"
		haha5.Value = true
	end
	local function loadcommands()
		local coms = {}
		local e = {"Mythic","Legendary","Rare","Uncommon","Common","Default"}
		for i,v in pairs(Commands:GetChildren()) do
			table.insert(coms,v.Name)
		end
		table.sort(coms, function(a, b)
			return table.find(e, require(game.ReplicatedStorage.CommandsModule)[a]["Rarity"]) < table.find(e, require(game.ReplicatedStorage.CommandsModule)[b]["Rarity"])
		end)
		for _,com in pairs(coms) do
			local newbutton = game.ReplicatedStorage.Example:Clone()
			newbutton.Name = com
			newbutton.Image = require(game.ReplicatedStorage.CommandsModule)[com]["Image"]
			newbutton.ComName.Text = require(game.ReplicatedStorage.CommandsModule)[com]["Name"]
			newbutton.ComName.BackgroundColor3 = require(game.ReplicatedStorage.RarityColours)[require(game.ReplicatedStorage.CommandsModule)[com]["Rarity"]]
			newbutton.Parent = player.PlayerGui.ScreenGui.Inventory.ScrollingFrame
		end
	end
	Commands.ChildAdded:Connect(function(value)
		repeat wait() until value.Name ~= "Value"
		local newbutton = game.ReplicatedStorage.Example:Clone()
		newbutton.Name = value.Name
		newbutton.Image = require(game.ReplicatedStorage.CommandsModule)[value.Name]["Image"]
		newbutton.ComName.Text = require(game.ReplicatedStorage.CommandsModule)[value.Name]["Name"]
		newbutton.ComName.BackgroundColor3 = require(game.ReplicatedStorage.RarityColours)[require(game.ReplicatedStorage.CommandsModule)[value.Name]["Rarity"]]
		newbutton.Parent = player.PlayerGui.ScreenGui.Inventory.ScrollingFrame
	end)
	spawn(function()
		while wait() do
			if Exp.Value >= ReqExp.Value then
				Level.Value+=1
				Exp.Value-=ReqExp.Value
				if Level.Value<=15 then
				   ReqExp.Value = require(game.ReplicatedStorage.LevelModule)[Level.Value]
				else
					ReqExp.Value*=1.1
				end
			end
		end
	end)
	local function loadnametags()
		local gui = game.ReplicatedStorage.UserNameGui:Clone()
		gui.Parent = player.Character.Head
		gui.PlayerName.Text = player.Name
	    if IsVIP.Value then
			gui.PlayerName.TextColor3 = Color3.new(1, 0.666667, 0)
			gui.PlayerName.Text = gui.PlayerName.Text.."(VIP)"
		end
		if player.Name == "ImNotInfected13" then
			gui.PlayerName.TextColor3 = Color3.new(0, 0.666667, 1)
			gui.PlayerName.Text = gui.PlayerName.Text.."(Creator)"
			if #Commands:GetChildren() == 5 then
				for _,command in pairs(require(game.ReplicatedStorage.CommandsModule)) do
					if Commands:FindFirstChild(command.Name) == nil then
						local newvalue = Instance.new("BoolValue",Commands)
						newvalue.Name = command.Name
					end
				end
			end
		elseif player.Name == "englishnow2" then
			gui.PlayerName.TextColor3 = Color3.new(1, 0, 0)
			gui.PlayerName.Text = gui.PlayerName.Text.."(Elite)"
			if #Commands:GetChildren() == 5 then
				for _,command in pairs(require(game.ReplicatedStorage.CommandsModule)) do
					if Commands:FindFirstChild(command.Name) == nil then
						local newvalue = Instance.new("BoolValue",Commands)
						newvalue.Name = command.Name
					end
				end
			end
		end
		spawn(function()
			repeat wait() until player.Character:FindFirstChild("Humanoid")
			while wait() do
				if player then
				if player.Character then
				if player.Character.Humanoid.Health ~= 0 then
					if player.IsOnMatch.Value == false then
					gui.Level.Text = "Level "..Level.Value
					else
						break
					end
				else
					break
				end
				end
				end
			end
		end)
	end
	repeat wait() until player.Character
	loadcommands()
	loadnametags()
	player.CharacterAdded:Connect(function()
		wait()
		IsOnMatch.Value = false
		loadcommands()
		loadnametags()
	end)
end)
local function onplayerleft(player)
	local coms = {}
	for _,command in pairs(player.Commands:GetChildren()) do
		table.insert(coms,{
			Name = command.Name,
			Value = command.Value
		})
	end
	plrdata:SetAsync(player.UserId,{player.Coins.Value,player.ReqExp.Value,player.Exp.Value,coms})
	plrdatasorted1:SetAsync(player.UserId,player.Level.Value)
	plrdatasorted2:SetAsync(player.UserId,player.Kills.Value)
end
game.Players.PlayerRemoving:Connect(function(player)
	onplayerleft(player)
end)
game:BindToClose(function()
	for _,player in pairs(game.Players:GetPlayers()) do
		onplayerleft(player)
	end
end)
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player,arg1,arg2)
	if arg1 == "Check" then
		if player.Commands:FindFirstChild(arg2) then
			player.PlayerGui.ScreenGui.Inventory.TextButton.Checking.Value = arg2
		end
	elseif arg1 == "Interact" then
		if player.Commands:FindFirstChild(arg2) then
			player.Commands[arg2].Value = not player.Commands[arg2].Value
		end
	elseif arg1 == "Battle" and (arg2 == "Normal" or arg2 == "Default Commands Only") then
		if player.IsOnMatch.Value == false then
			player.IsOnMatch.Value = true
			player.Character.Humanoid.WalkSpeed = 16
			game.ReplicatedStorage.RemoteEvent:FireClient(player,"Black",0)
			wait(.8)
			if arg2 == "Normal" then
			player.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.Baseplate.Position+Vector3.new(0,16,0)+Vector3.new(math.random(-150,150),0,math.random(-150,150)))
			local paneladmin = game.ReplicatedStorage["Admin Panel"]:Clone()
			paneladmin.Parent = player.Backpack
			else
				player.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.Baseplate2.Position+Vector3.new(0,16,0)+Vector3.new(math.random(-150,150),0,math.random(-150,150)))
				local paneladmin = game.ReplicatedStorage["Admin Panel(default only)"]:Clone()
				paneladmin.Parent = player.Backpack	
			end
			local punch = game.ReplicatedStorage.Punch:Clone()
			punch.Parent = player.Backpack
			player.Character.Head.UserNameGui:Destroy()
			game.ReplicatedStorage.RemoteEvent:FireClient(player,"Black",1)
		end
	end
end)
1 Like

I sent you a PM if it works mark this comment.
if so post it if you like, but it’s really long.