I am trying to use the key of a dictionary as a string for checking the DataStore entry of the same key name, but I cant seem to get the keys name to work as a string. When i print the keys name, i get the name printed out fine, but when i try to use it in a reference to that DataStore key, i get an error that says:
[Bad argument #1 to SaveData::Get() (string expected)]
Here is the code:
--// Recipes: we currently only have one recipe,
local recipes = {
Tacos = {
Lettuce = 1,
Shells = 1,
Cheese = 1,
Meat = 1}
}
function tycoonTools.cookTacos(player)
local currentRecipe = recipes.Tacos
local saveData = PlayerDataStore:GetSaveData(player)
local checkRecipe = {}
for i,v in pairs (currentRecipe) do
print(i)
local currentValue = saveData:Get(i)
if currentValue >= v then
checkRecipe[i] = true
else checkRecipe[i] = false
end
end
So whats it trying to do is match the value in saveData and compare it to the value of currentRecipe and if that value od currentRecipe is greater than or equal to the one in saveData, it will set a value of true in the table checkRecipe.
The keys in the saveData table match the keys in the currentRecipes table but I get the error on this line:
local currentValue = saveData:Get(i)
I guess it needs a string and i have tried things like tostring but i cant get it to work.
Try doing print(type(i)), if it shows string then you should be fine, if it shows anything else but that you can try using tostring(i) instead of just doing i.
Itâs very likely an issue with the :Get() method, so itâd help if you could post that. Also if possible itâd help if you print the result of type() from the arguments Get receives.
Iâm having a hard time finding the exact method inside the DataStore script because I didnât make it, I will keep looking.
However, I have the same method workign in another function just fine:
function scoreTools.addDataScore(player,name,value)
local saveData = PlayerDataStore:GetSaveData(player)
local currentCount = saveData:Get(name)
if not currentCount then
currentCount = 0
end
currentCount = currentCount + value
saveData:Set(name,currentCount)
end
-- Getter and setter function to manipulate keys
-- for this player.
function this:Get(key)
me.debugPrint("(".. this.userId .. ") GETTING " .. tostring(key));
if type(key) ~= 'string' then
error("Bad argument #1 to SaveData::Get() (string expected)", 2)
end
if DEBUG then
print("SaveData<"..this.userId..">::Get("..key..")")
end
markAsTouched(key)
local value = this.dataSet[key]
if value == nil and DESERIALIZE[key] then
-- If there's no current value, and the key
-- has serialization, then we should get the
-- null deserialized state.
local v = DESERIALIZE[key](nil)
-- Note: we don't markAsDirty here, that's
-- intentional, as we don't want to save
-- if we don't have to, and we don't need
-- to here, as Deserialize(key, nil) should
-- return back the same thing every time.
-- However, we still need cache the value,
-- because deserialize(key, nil) might still
-- be expensive or the caller might expect
-- the same reference back each time.
this.dataSet[key] = v
return v
else
return value
end
end
Although I am not sure if itâs any help, from the example you gave it does seem like youâre missing an âendâ to close the scope. For the loop or function it seems. Other than that it should work, it works when I try it in a basic test setup.