Hello, I wrote this script to test saving any leaderstats value and unfortunately it will not save the 'Schmeckles' value. Is anybody experienced enough with DataStore that they could point out what I've done wrong? I left the script in Server Script Storage.
local DataStore = game:GetService("DataStoreService"):GetDataStore('Currency')
game.Players.PlayerAdded:connect(function(player)
local stats = Instance.new("IntValue", player)
stats.Name = "leaderstats"
local Schmeckles = Instance.new("IntValue", stats)
Schmeckles.Name = "Schmeckles"
local key = "player-"..player.userId
local SavedValues = DataStore:GetAsync(key)
if SavedValues then
Schmeckles.Value = SavedValues[1]
else
DataStore:SetAsync(key, Schmeckles.Value)
end
end)
game:BindToClose(function()
game.Players.PlayerRemoving:connect(function(player)
local key = "player-"..player.userId
local Schmeckles = player.leaderstats.Schmeckles.Value
DataStore:SetAsync(key, Schmeckles)
end)
end)

5 comments
try removing .Value from line 27 and adding it to Schmeckles on line 28
Why are you connecting the PlayerRemoving event when the server is shutting down? You are also saving the value but accessing it trying to load it as a table.
Provide us with the error.
It's all I could grasp from the wiki and numerous videos, thankyou for the comments I'll try them out. Light the point of this question was I could not see any visible error because it's a logic error.
kingdom could you elaborate on me loading the array? I can't see how I would solve this