I have edited it however this still doesn’t work.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local plrStats = {
inventory = {
weapons = {},
materials = {}
},
["Credits"] = 0,
["Summon Tokens"] = 0,
["Legendary Pity"] = 0,
["Epic Pity"] = 0,
["Element"] = 0, --1(Fire),2(Water),3(Ice),4(Nature),5(Lightning),6(Earth),7(Light),8(Dark),9(Light),10(Psychic)
--Customizable Stats
["Skin Tone"] = "Default",
["Hair"] = "None",
["Hat"] = "None",
["Shirt"] = "Default",
["Pants"] = "Default",
["Shoes"] = "Default",
--Base Stats as Integers
["Level"] = 1,
["Base HP"] = 100,
["Base Atk"] = 10,
["Base Def"] = 0,
["Base MP"] = 10,
["Base Crit %"] = .05,
["Base Crit Dmg"] = .5,
["Base Shield DMG Resist"] = 0.00,
--Added Stats
["HP Bonus"] = 0,
["Atk Bonus"] = 0,
["Def Bonus"] = 0,
["MP Bonus"] = 0,
["Crit% Bonus"] = 0.00,
["Crit Dmg Bonus"] = 0.00,
["Shield DMG Resist Bonus"] = 0,
--Equipped Items
["Current Sword"] = 1,
["Current Shield"] = 1
}
local PLAYER
game.Players.PlayerAdded:Connect(function(player)
PLAYER = player
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local displayLevel = Instance.new("IntValue")
displayLevel.Name = "Level"
displayLevel.Value = plrStats.Level
displayLevel.Parent = leaderstats
local folder = Instance.new("Folder")
folder.Name = "PlayerItems"
folder.Parent = player
local weapons = Instance.new("Folder")
weapons.Name = "Weapons"
weapons.Parent = folder
for i,v in ipairs(plrStats.inventory.weapons) do
local vClone = v:Clone()
v.Parent = weapons
end
local summonTokens = Instance.new("IntValue")
summonTokens.Name = "Summon Tokens"
summonTokens.Value = plrStats["Summon Tokens"]
summonTokens.Parent = player.PlayerItems
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-DATA")
end)
if success then
print("Data Loaded Successfully")
--credits.Value = data
else
warn("Error loading Save Data",errormessage)
end
end)
local player = PLAYER
game:BindToClose(function()
local success, errormessage = pcall(function()
plrStats.inventory.weapons = {player.PlayerItems.Weapons}
plrStats.Level.Value = player.leaderstats.Level.Value
plrStats["Summon Tokens"].Value = player.PlayerItems["Summon Tokens"].Value
myDataStore:SetAsync(player.UserId.."-DATA", plrStats)
end)
if success then
print("Data Successfully Saved.")
else
warn("There was an Error Saving Data",errormessage)
end
end)
Now that the function that saves the data can no longer use the player parameter, I’m not sure if it’s working. I tried making a variable that defines the player in the PlayerAdded function with an external variable, allowing the player to use that variable by the time the BindToClose fires, however again, it still isn’t functioning.