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
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)
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