Hey, basically I have no idea what I’m doing, and just copy-pasted it from another one of my games where data saving works, but it doesn’t in this game.
Here’s my script. Can someone tell me what the issue is?
local ProfileTemplate = {
PlayerStats = {Kills = 0, Wins = 0, Level = 1, Exp = 0, Coins = 0},
Weapons =
{
WeaponStats = {EquippedKnife = "Classic", EquippedGun = "Classic", EquippedSword = "Classic"},
Knives = {Classic = 1, Bronze = 0, Silver = 0, Gold = 0, Platinum = 0, Lapis = 0, Obsidian = 0, Diamond = 0, Amethyst = 0, Ruby = 0, Emerald = 0, Turqoise = 0, Flame = 0, Frost = 0, Shadow = 0, Lunar = 0, Supernova = 0, Void = 0},
Guns = {Classic = 1, Bronze = 0, Silver = 0, Gold = 0, Platinum = 0, Lapis = 0, Obsidian = 0, Diamond = 0, Amethyst = 0, Ruby = 0, Emerald = 0, Turqoise = 0, Flame = 0, Frost = 0, Shadow = 0, Lunar = 0, Supernova = 0, Void = 0},
Swords = {Classic = 1, Bronze = 0, Silver = 0, Gold = 0, Platinum = 0, Lapis = 0, Obsidian = 0, Diamond = 0, Amethyst = 0, Ruby = 0, Emerald = 0, Turqoise = 0, Flame = 0, Frost = 0, Shadow = 0, Lunar = 0, Supernova = 0, Void = 0}
}
}
----- Loaded Modules -----
local ProfileService = require(script.ProfileService)
----- Private Variables -----
local Players = game:GetService("Players")
local ProfileStore = ProfileService.GetProfileStore("PlayerData", ProfileTemplate)
local Profiles = {}
local function DoSomethingWithALoadedProfile(Player, Profile)
local PlayerStats = Player:WaitForChild("PlayerStats")
local Weapons = Player:WaitForChild("Weapons")
PlayerStats.Kills.Value = Profile.Data.PlayerStats.Kills
PlayerStats.Wins.Value = Profile.Data.PlayerStats.Wins
PlayerStats.Level.Value = Profile.Data.PlayerStats.Level
PlayerStats.Exp.Value = Profile.Data.PlayerStats.Exp
PlayerStats.Coins = Profile.Data.PlayerStats.Coins
Weapons.EquippedKnife = Profile.Data.Weapons.WeaponStats.EquippedKnife
Weapons.EquippedGun = Profile.Data.Weapons.WeaponStats.EquippedGun
Weapons.EquippedSword = Profile.Data.Weapons.WeaponStats.EquippedSword
PlayerStats.Kills.Changed:Connect(function(Value)
Profile.Data.PlayerStats.Kills = Value
end)
PlayerStats.Wins.Changed:Connect(function(Value)
Profile.Data.PlayerStats.Wins = Value
end)
PlayerStats.Level.Changed:Connect(function(Value)
Profile.Data.PlayerStats.Level = Value
end)
PlayerStats.Exp.Changed:Connect(function(Value)
Profile.Data.PlayerStats.Exp = Value
end)
PlayerStats.Coins.Changed:Connect(function(Value)
Profile.Data.PlayerStats.Coins = Value
end)
for _, Knife in Weapons.Knives:GetChildren() do
Knife.Changed:Connect(function(Value)
Profile.Data.Weapons.Knives[Knife.Name] = Value
end)
end
for _, Gun in Weapons.Guns:GetChildren() do
Gun.Changed:Connect(function(Value)
Profile.Data.Weapons.Guns[Gun.Name] = Value
end)
end
for _, Sword in Weapons.Swords:GetChildren() do
Sword.Changed:Connect(function(Value)
Profile.Data.Weapons.Swords[Sword.Name] = Value
end)
end
end
local function PlayerAdded(Player)
local Profile = ProfileStore:LoadProfileAsync(tostring(Player.UserId))
if Profile ~= nil then
Profile:AddUserId(Player.UserId)
Profile:Reconcile()
Profile:ListenToRelease(function()
Profiles[Player] = nil
Player:Kick()
end)
if Player:IsDescendantOf(Players) == true then
Profiles[Player] = Profile
DoSomethingWithALoadedProfile(Player, Profile)
else
Profile:Release()
end
else
Player:Kick()
end
end
----- Initialize -----
for _, Player in ipairs(Players:GetPlayers()) do
task.spawn(PlayerAdded, Player)
end
----- Connections -----
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(function(Player)
local Profile = Profiles[Player]
if Profile ~= nil then
Profile:Release()
end
end)