I am having an error: ServerScriptService.Data.Stats:45: attempt to index nil with ‘Confetti’
I do not know what I am doing wrong, here is my data manager script:
local Players = game:GetService("Players")
local ProfileService = require(game.ReplicatedStorage.ProfileService)
local dateTable = os.date("*t", os.time())
local keyForToday = "Y:"..dateTable["year"].." M:"..dateTable["month"].." D:"..dateTable["day"]
local ProfileStore = ProfileService.GetProfileStore(
"Player",
{
["Champion Wins"] = 0;
Wins = 0;
Coins = 0;
Effects = {
Confetti = 0;
Money = 0;
};
}
)
local Profiles = {}
local function setData(player)
local profile = Profiles[player]
if profile then
local data = profile.Data
if data then
data["Champion Wins"] = player["Champion Wins"].Value
data.Wins = player.leaderstats.Wins.Value
data.Coins = player.leaderstats.Coins.Value
data.Effects["Confetti"] = player.Effects.Confetti.Value
data.Effects["Money"] = player.Effects.Money.Value
end
end
end
local function onPlayerAdded(player)
local profile = ProfileStore:LoadProfileAsync(
"Player_" .. player.UserId,
"ForceLoad"
)
if profile then
profile:ListenToRelease(function()
Profiles[player] = nil
player:Kick("Session ended.")
end)
if player:IsDescendantOf(Players) then
Profiles[player] = profile
game.ReplicatedStorage.Loaded:Fire(player)
else
profile:Release()
end
else
player:Kick("Data failed to load.")
end
end
local function onPlayerRemoving(player)
local profile = Profiles[player]
setData(player)
if profile then
profile:Release()
end
end
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers())do
local profile = Profiles[player]
setData(player)
if profile then
profile:Release()
end
end
end)
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
local DataManager = {}
function DataManager:Get(player)
local profile = Profiles[player]
if profile then
return profile.Data
end
end
function DataManager:Set(player)
local profile = Profiles[player]
if profile then
local data = profile.Data
if data then
data["Champion Wins"] = player["Champion Wins"].Value
data.Wins = player.leaderstats.Wins.Value
data.Coins = player.leaderstats.Coins.Value
data.Effects["Confetti"] = player.Effects.Confetti.Value
data.Effects["Money"] = player.Effects.Money.Value
end
end
end
function DataManager:Clear(Id)
ProfileStore:WipeProfileAsync("Player_"..Id)
end
return DataManager
Here is my stat script:
local DataManager = require(game.ReplicatedStorage.DataManager)
local Players = game:GetService("Players")
local statTemplate = {
["Champion Wins"] = 0;
Wins = 0;
Coins = 0;
}
local effectsTemplate = {
Confetti = 0;
Money = 0;
}
function PlayerLoaded(player)
local data = DataManager:Get(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
for name, value in pairs(statTemplate)do
local stat = Instance.new("IntValue")
stat.Name = name
stat.Value = data[name]
if name == "Wins"then
stat.Parent = leaderstats
end
if name == "Coins"then
stat.Parent = leaderstats
end
if name == "Champion Wins"then
stat.Parent = player
end
end
local effects = Instance.new("Folder", player)
effects.Name = "Effects"
for name, value in pairs(effectsTemplate)do
local stat = Instance.new("IntValue")
stat.Name = name
stat.Value = data.Effects[name]
stat.Parent = effects
end
local new = Instance.new("BoolValue",player)
new.Name = "Loaded"
new.Value = true
end
game.ReplicatedStorage.Loaded.Event:Connect(PlayerLoaded)
How do I go about solving this error?