I'm making a DataStore that saves if a quest is complete or not by saving a Boolean value.
This is the error:
Workspace.LoadQuestScript:59: attempt to index local 'savedvalues' (a boolean value)
This is the script I am having trouble with:
wait(1) local DataStore = game:GetService("DataStoreService"):GetDataStore("FolderStore") game.Players.PlayerAdded:connect(function(player) local folder = Instance.new("Folder") folder.Parent = player folder.Name = "CompletedQuests" local quest1 = Instance.new("BoolValue") quest1.Parent = folder quest1.Name = "QUEST1" local quest2 = Instance.new("BoolValue") quest2.Parent = folder quest2.Name = "QUEST2" local quest3 = Instance.new("BoolValue") quest3.Parent = folder quest3.Name = "QUEST3" local quest4 = Instance.new("BoolValue") quest4.Parent = folder quest4.Name = "QUEST4" local quest5 = Instance.new("BoolValue") quest5.Parent = folder quest5.Name = "QUEST5" local quest6 = Instance.new("BoolValue") quest6.Parent = folder quest6.Name = "QUEST6" local quest7 = Instance.new("BoolValue") quest7.Parent = folder quest7.Name = "QUEST7" local quest8 = Instance.new("BoolValue") quest8.Parent = folder quest8.Name = "QUEST8" local quest9 = Instance.new("BoolValue") quest9.Parent = folder quest9.Name = "QUEST9" local quest10 = Instance.new("BoolValue") quest10.Parent = folder quest10.Name = "QUEST10" local key = "player-"..player.userId print("Added "..key) local savedvalues = DataStore:GetAsync(key) --Error cause(?) if savedvalues then print("Saved") quest1.Value = savedvalues[1] --Error happens here. quest2.Value = savedvalues[2] quest3.Value = savedvalues[3] quest4.Value = savedvalues[4] quest5.Value = savedvalues[5] quest6.Value = savedvalues[6] quest7.Value = savedvalues[7] quest8.Value = savedvalues[8] quest9.Value = savedvalues[9] quest10.Value = savedvalues[10] print("Successfully loaded Data for "..key.."!") else print("Saving") local valuestosave = {quest1.Value, quest2.Value, quest3.Value, quest4.Value, quest5.Value, quest6.Value, quest7.Value, quest8.Value, quest9.Value, quest10.Value} DataStore:SetAsync(key, valuestosave) print("Couldn't find Data for "..key..". Making new Data...") end end)
Any help is appreciated. I have had a lot of problems with DataStores over the past 4 days.
Instead of doing each one, make a for loop:
-- you do not need quest1 variables etc. for i,v in pairs (savedvalues) do local find = folder:FindFirstChild("QUEST"..i) if find then find.Value = v end end
EDIT:
to make sure it doesn't fail
if savevalues[1] then -- code else -- save end
Sorry if it didn't fix your problem -_-
The error you're getting suggests that savedvalues
was either true
or false
-- you probably accidentally saved one of the Quest values to it before.
You could try clearing out the data, or you could just more robustly check:
You want to reject nil
, true
, and false
-- you expect a table if they had a value. You can use type
to check this:
if type(savedvalues) == "table" then -- then there is data to load
You aren't ever saving data -- the only time you save is immediately when they join (which will be all falses).
You should save the data periodically / when they leave, for example, on PlayerRemoving
:
function save(player) local quests = player.CompletedQuests local toSave = {} for i = 1, #quests:GetChildren() do toSave[i] = quests:FindFirstChild("quest" .. i).Value end local key = "player-"..player.userId DataStore:SetAsync(key, toSave) end game.Players.PlayerRemoving:connect(save)