I am attempting to create a data sore script in which to save just three leaderstats. Everything seems to be working fine except when it gets to line 22, where the script crashes. I know I am bad at errors, which I am still learning thoroughly, but can anyone explain to me why this problem occurred?
This is the error:
Workspace.PlayerEntered:22: attempt to index local ‘savedValues’ (a number value)
local DataStore = game:GetService("DataStoreService"):GetDataStore("Stats")
game.Players.PlayerAdded:connect(function(player)
local stats = Instance.new("IntValue", player)
stats.Name = "leaderstats"
local win = Instance.new("IntValue", stats)
win.Name = "Wins"
local kill = Instance.new("IntValue", stats)
kill.Name = "Kills"
local death = Instance.new("IntValue", stats)
death.Name = "Deaths"
local key = "player-"..player.UserId
local savedValues = DataStore:GetAsync(key)
if savedValues then
win.Value = savedValues[1]
kill.Value = savedValues[2]
death.Value = savedValues[3]
else
local winToSave = (win.Value)
local killToSave = (kill.Value)
local deathToSave = (death.Value)
DataStore:SetAsync(key, winToSave)
DataStore:SetAsync(key, killToSave)
DataStore:SetAsync(key, deathToSave)
end
end)
This code is the save.
local DataStore = game:GetService("DataStoreService"):GetDataStore("Stats")
game.Players.PlayerRemoving:connect(function(player)
local short = player.leaderstats
local key = "player-"..player.UserId
local winToSave = (short.Wins.Value)
local killToSave = (short.Kills.Value)
local deathToSave = (short.Deaths.Value)
DataStore:SetAsync(key, winToSave)
DataStore:SetAsync(key, killToSave)
DataStore:SetAsync(key, deathToSave)
end)

3 comments
Did you forget to add a SetAsync() code block? Which saves the code.
On line 17 of the first script, and line 5 of your second script, I think it's suppose to be userId, not UserId
you cant access datastores in studio, unless u go into edit mode so it should work in game