I want it so Pebbles and Value both save and load, but when I join the game the output says 20:40:40.729 - ServerScriptService.leaderstats:21: attempt to index local 'SavedData' (a number value)
local DataStore = game:GetService("DataStoreService"):GetDataStore('RockData12')
game.Players.PlayerAdded:Connect(function(player)
local key = player.UserId
local leaderstats = Instance.new("Folder")
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local Pebbles = Instance.new("IntValue")
Pebbles.Name = 'Pebbles'
Pebbles.Value = 0
Pebbles.Parent = leaderstats
local level = Instance.new("IntValue")
level.Name = 'Level'
level.Value = 0
level.Parent = leaderstats
local SavedData = DataStore:GetAsync(key)
if SavedData then
player.leaderstats.Pebbles.Value = SavedData[1]
player.leaderstats.Level.Value = SavedData[2]
else
local StartValues = {Pebbles.Value, level.Value}
DataStore:SetAsync(key, StartValues)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = player.UserId
local CurrentValue = {player.leaderstats.Pebbles.Value}
local LevelValue = {player.leaderstats.Level.Value}
DataStore:SetAsync(key, CurrentValue[1], LevelValue[2])
end)
game:BindToClose(function()
for i,v in pairs(game.Players:GetPlayers()) do
if v then
v:Kick()
end
end
wait(3)
end)

1 comment
Do not forget to accept my answer if it helps out.