I’ve recently coded this and for some reason I don’t seem to be able to get the stored data or it just doesn’t save my data!
ModuleScript:
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local SavingDataStore = DataStoreService:GetDataStore("Saves1")
local PlayersStats = {}
function GetNewStats()
if RunService:IsServer() then
local Stats = {
Money = 200,
Level = 0,
Exp = 0
}
return Stats
end
end
function Add(Key, Stats)
if RunService:IsServer() then
if Key and Stats then
PlayersStats[Key] = Stats
end
end
end
function Remove(Key)
if RunService:IsServer() then
if Key then
local PlayerStats = PlayersStats[Key]
if PlayerStats then
PlayersStats[Key] = nil
end
end
end
end
function Set(Key, Stats)
if RunService:IsServer() then
if Key and Stats then
local Player = Players:GetPlayerByUserId(Key)
if Player then
local Success, ErrorMessage = pcall(function()
SavingDataStore:SetAsync(Key, Stats)
end)
if Success then
PlayersStats[Key] = Stats
warn("Successfully saved " .. Player.Name .. "'s stats!")
else
warn(ErrorMessage)
end
end
end
end
end
function Get(Key)
if RunService:IsServer() then
if Key then
local Player = Players:GetPlayerByUserId(Key)
if Player then
local SavedStats
local Success, ErrorMessage = pcall(function()
local Saved = SavingDataStore:GetAsync(Key)
SavedStats = Saved
end)
if Success then
warn("Successfully received " .. Player.Name .. "'s stats!")
return SavedStats
else
warn(ErrorMessage)
return false
end
end
end
end
end
function GetAllStats()
if RunService:IsServer() then
return PlayersStats
end
end
return {
Add = Add,
Remove = Remove,
Set = Set,
Get = Get,
GetAll = GetAllStats,
GetDefault = GetNewStats
}
Server Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local GameFolder = ReplicatedStorage:WaitForChild("Game")
local DataStore = require(GameFolder:WaitForChild("DataStore"))
Players.PlayerAdded:Connect(function(Player)
local PlayerId = Player.UserId
local DefaultStats = DataStore.GetDefault()
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Value = DefaultStats.Money
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Value = DefaultStats.Level
local Exp = Instance.new("IntValue")
Exp.Name = "Exp"
Exp.Value = DefaultStats.Exp
Money:GetPropertyChangedSignal("Value"):Connect(function()
local New = {
Money = Money.Value,
Level = Level.Value,
Exp = Exp.Value
}
DataStore.Set(PlayerId, New)
end)
Level:GetPropertyChangedSignal("Value"):Connect(function()
local New = {
Money = Money.Value,
Level = Level.Value,
Exp = Exp.Value
}
DataStore.Set(PlayerId, New)
end)
Exp:GetPropertyChangedSignal("Value"):Connect(function()
local New = {
Money = Money.Value,
Level = Level.Value,
Exp = Exp.Value
}
DataStore.Set(PlayerId, New)
end)
local PlayerStats = DataStore.Get(PlayerId)
if PlayerStats then
DataStore.Add(PlayerId, PlayerStats)
else
PlayerStats = DefaultStats
DataStore.Set(PlayerId, DefaultStats)
end
Money.Value = PlayerStats.Money
Leaderstats.Parent = Player
Money.Parent = Leaderstats
Player.Team = Teams.Prisoner
end)
Players.PlayerRemoving:Connect(function(Player)
local PlayerId = Player.UserId
local PlayerStats
local AllStats = DataStore.GetAll()
if AllStats[PlayerId] then
PlayerStats = AllStats[PlayerId]
end
if PlayerStats then
DataStore.Set(PlayerId, PlayerStats)
end
DataStore.Remove(PlayerId)
end)
game:BindToClose(function()
for _, Player in pairs(Players:GetPlayers()) do
if Player and Player:IsA("Player") then
local PlayerId = Player.UserId
local PlayerStats
local AllStats = DataStore.GetAll()
if AllStats[PlayerId] then
PlayerStats = AllStats[PlayerId]
end
if PlayerStats then
DataStore.Set(PlayerId, PlayerStats)
end
DataStore.Remove(PlayerId)
end
end
end)
Any help will be considered.