For some reason the data in my datastore wont save when I go to leave the server, how do I fix this?
local Data = game:GetService("DataStoreService"):GetDataStore("WildHunt")
game.Players.PlayerAdded:Connect(function(Player)
-----------------------------------------------------------------------------------Leaderboard
local LB = Instance.new("Folder",Player)
LB.Name = "leaderstats"
local Coins = Instance.new("IntValue",LB)
Coins.Name = "Coins"
local Level = Instance.new("IntValue",LB)
Level.Name = "Level"
-----------------------------------------------------------------------------------Stats
local Stats = Instance.new("Folder",Player)
Stats.Name = "Stats"
local Exp = Instance.new("IntValue",Stats)
Exp.Name = "Exp"
local MeatCount = Instance.new("IntValue",Stats)
MeatCount.Name = "MeatCount"
local CookedMeatCount = Instance.new("IntValue",Stats)
CookedMeatCount.Name = "CookedMeatCount"
local HideCount = Instance.new("IntValue",Stats)
HideCount.Name = "HideCount"
-----------------------------------------------------------------------------------
local Key = "Player-"..Player.UserId
local SavedValues = Data:GetAsync(Key)
-----------------------------------------------------------------------------------
print("Loading Data...")
if SavedValues then
Coins.Value = SavedValues[1]
Level.Value = SavedValues[2]
Exp.Value = SavedValues[3]
MeatCount.Value = SavedValues[4]
CookedMeatCount.Value = SavedValues[5]
HideCount.Value = SavedValues[6]
else
local ValuesToSave = {
Coins.Value,
Level.Value,
Exp.Value,
MeatCount.Value,
CookedMeatCount.Value,
HideCount.Value,
}
Data:SetAsync(Key, ValuesToSave)
end
print("Data Loaded!")
-----------------------------------------------------------------------------------
end)
game.Players.PlayerRemoving:Connect(function(Player)
print("Saving Data")
local Key = "Player-"..Player.UserId
local ValuesToSave = {
Player.leaderstats.Coins.Value,
Player.leaderstats.Level.Value,
Player.Stats.Exp.Value,
Player.Stats.MeatCount.Value,
Player.Stats.CookedMeatCount.Value,
Player.Stats.HideCount.Value,
}
Data:SetAsync(Key, ValuesToSave)
print("Saved!")
end)