[solved] DataStore is not saving

Hello, I’m working on a DataStore Module for my new game and it wasn’t actually saving any data also in the output there weren’t any errors. If anyone can help that would be great!

Here’s the code:

-- << Variables >> --

local DataStoreService = game:GetService("DataStoreService")
local PlayerStats = DataStoreService:GetDataStore("PlayerStats3")
local DataStore = {}
DataStore.__index = DataStore

-- << Functions >> --

function DataStore:SaveData(Settings, Ban, LastSession)
	if Settings then self.Settings = Settings end
	if Ban then self.Ban = Ban end
	if LastSession then self.LastSession = LastSession end
	local Success, Returned
	local function saveData()
		Success, Returned = pcall(function()
			return PlayerStats:UpdateAsync(self.Id, function(PreviousInfo)
				return {
					["Settings"] = self.Settings;
					["Ban"] = self.Ban;
					["LastSession"] = self.LastSession
				}
			end)
		end)
		if not Success then
			warn(Returned)
			task.wait(.5)
			saveData()
		end
	end
	saveData()
end

function DataStore:GetData()
	local Success, Returned
	local function getData()
		Success, Returned = pcall(function()
			local PlayerData = PlayerStats:GetAsync(self.Id)
			if PlayerData == nil then
				self.Settings = {MotionBlur = true, Brightness = 0.2, Graphics = "Normal"}
				self.Ban = {Status = false, Type = "", Time = {Then = 0, Length = 0}, Reason = "", UnbanTimes = 0}
				self.LastSession = {JobId = 0, Time = {Then = 0, Length = 0}, CanRejoin = false}
				self.HasPlayed = false
			else
				if not PlayerData["Settings"] then self.Settings = {MotionBlur = true, Brightness = 0.2, Graphics = "Normal"} else self.Settings = PlayerData["Settings"] end
				if not PlayerData["Ban"] then self.Ban = {Status = false, Type = "", Time = {Then = 0, Length = 0}, Reason = "", UnbanTimes = 0} else self.Ban = PlayerData["Ban"] end
				if not PlayerData["LastSession"] then self.LastSession = {JobId = 0, Time = {Then = 0, Length = 0}, CanRejoin = false} else self.LastSession = PlayerData["LastSession"] end
				self.HasPlayed = true
			end
		end)
		if not Success then
			warn(Returned)
			task.wait(.5)
			getData()
		end
	end
	getData()
end

function DataStore.new(Player)
	local self = setmetatable({}, DataStore)
	self.Id = Player.UserId
	self.Name = Player.Name
	return self
end

return DataStore
2 Likes

Try removing all caps from like “Settings”. That could be the reason, I haven’t been doing ds for a long time. I could be confused with mongoDB.

I already tried that out but that isn’t the case, because nothing changes like before any other ideas?

Enable this setting in secruity:

Is on but still not saving any data

have you tested it in game?

try checking the docs for data saving could help.Data Stores . Also test in a public published place like said on top of me.

Yes in Studio and in game, also for studio I have a wait(3.5) in game:BindToClose()

Try adding some print statements and points of the script and see if some aren’t printing.

Maybe there is something wrong with the server script, but I also couldn’t find anything in there

ServerScript:

-- << Server Tables >> --

_G.TemporaryBanned = {}
_G.Protected = {997655324}
_G.AdminPermission = {997655324}

-- << Variables >> --

local TeleportService = game:GetService("TeleportService")
local MessagingService = game:GetService("MessagingService")
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerStats = DataStoreService:GetDataStore("PlayerStats3")
local Donations = DataStoreService:GetOrderedDataStore("Donations")
local BadgeService = game:GetService("BadgeService")
local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerModules = ServerScriptService:WaitForChild("Modules")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")

-- << Modules >> --

local Tags = require(ServerModules:WaitForChild("Tags"))

-- << Player Joins >> --

Players.PlayerAdded:Connect(function(Player)
	
	-- << Checking if server is locked >> --
	
	if ServerStorage.Server.ServerLocked.Value == true then
		if ServerStorage.Server.ServerLocked.Reason.Value == "" then
			return Player:Kick("\n\nThis Server has been locked!\n\nTry rejoining later again...\n")
		else
			return Player:Kick("\n\nThis Server has been locked!\n\nReason: " .. ServerStorage.Server.ServerLocked.Reason.Value .. "\nTry rejoining later again...\n")
		end
	end
	
	-- << Checking TemporaryBanned Table >> --
	
	if _G.TemporaryBanned[Player.UserId] then
		local Data = _G.TemporaryBanned[Player.UserId]
		return Player:Kick("\n\nYou have been Temporary server banned!\n\nBanned by: " .. Data[1] .. "\nReason: " .. Data[2] .. "\n")
	end
	
	-- << Check ServerStorage Data >> --
	
	if not ServerStorage:WaitForChild("Data"):FindFirstChild(Player.UserId) then
		local NewValue = Instance.new("BoolValue", ServerStorage:WaitForChild("Data"))
		NewValue.Name = Player.UserId
		NewValue.Value = true
	end
	
	-- << Local Variables/Modules >> --
	
	local DataStore = require(Modules:WaitForChild("DataStore"))
	local PlayerStorageModule = require(Modules:WaitForChild("Storage"))
	local PlayerData = DataStore.new(Player)
	local PlayerStorage
	PlayerData:GetData()
	PlayerStorage = PlayerStorageModule.new(Player, PlayerData)
	
	-- << Player Ban Status >> --
	
	if PlayerData.Ban.Status == true then
		if PlayerData.Ban.Type == "Permanently" then
			local ReservedCode = TeleportService:ReserveServer(9297639559)
			TeleportService:TeleportToPrivateServer(9297639559, ReservedCode, {Player}, nil)
			return Player:Kick("\nTeleporting...")
		elseif PlayerData.Ban.Type == "Temporary" then
			local TimeDiff = os.time() - PlayerData.Ban.Time.Then
			if TimeDiff < PlayerData.Ban.Time.Length then
				local ReservedCode = TeleportService:ReserveServer(9297639559)
				TeleportService:TeleportToPrivateServer(9297639559, ReservedCode, {Player}, nil)
				return Player:Kick("\nTeleporting...")
			else
				PlayerData.Ban.Status = false
				PlayerData.Ban.Type = ""
				PlayerData.Ban.Time.Then = 0
				PlayerData.Ban.Time.Length = 0
				PlayerData.Ban.Reason = ""
			end
		end
	end
	
	-- << Check if Player already played >> --
	
	if PlayerData.HasPlayed == false then
		local Success, Returned
		local function award()
			Success, Returned = pcall(function()
				return BadgeService:AwardBadge(Player.UserId, 2126083956)
			end)
			if not Success then
				warn(Returned)
				task.wait(.5)
				award()
			else
				print("[Server]: ✅ " .. Player.Name .. " [" .. Player.UserId .. "] has been awarded the Welcome Badge!")
			end
		end
		award()
	end
	
	-- << Player Leaving >> --
	
	Players.PlayerRemoving:Connect(function(PlayerLeaving)
		if Player == Player then
			if ServerStorage:WaitForChild("Server").DataSaving.Value == true and ServerStorage:WaitForChild("Data"):FindFirstChild(Player.UserId).Value == true then
				local NewTable = PlayerStorage:UpdateServerValues()
				PlayerData.Settings = NewTable.Settings
				PlayerData:SaveData(PlayerData.Settings, PlayerData.Ban, PlayerData.LastSession)
				ServerStorage:WaitForChild("Data"):FindFirstChild(Player.UserId):Destroy()
			end
		end
	end)
	
end)

-- << Studio Shutdown >> --

game:BindToClose(function()
	if RunService:IsStudio() then
		task.wait(3.5)
	end
end)

Is tags the data saving module*?

No the DataStore Module is in the PlayerAdded event

I thought you made the saving script into a module.
NVM I missed the line.

Yes it is a Module but you have to require the Module through a ServerScript or you cannot save any data

sorry didnt saw your edit lol

You probably do, but do you have this turned on?
image

All things are turned on but what does HTTP Requests have to do with data saving?

grafik

I don’t know what to do. I am going to ask my “friend”.

Alright, I haven’t found the error since 3 days now it is really annoying

He said:
“just use profile service”
and also he said:
“tbf I wouldn’t advise what he is doing”

Nvm I think I finally found the problem I should’ve done if PlayerLeaving == Player then instead of
if Player == Player then. Imma check if it is working now

Edit: It is working perfectly fine now

2 Likes