Problem with finding key in dictionary

Hello everybody! I’m trying to code an easy way to update variables into data store. My script is supposed to check if the retrieved datastore has the dictionary key it is looking for is there. When I try many iterations, this code seems to fail.

local data = {
	folder1 = {
		Var1 = 0,
	},
	folder2 = {
		String = 'StringValue',
		Var2 = 0,
	}
}

players.PlayerAdded:Connect(functionblahblahblah
dataforstore = datstore:GetAsync(Player.UserId)
        instance.new("Folder") -- this part works fine and im just putting it here for the sake of logic.
		for name, _ in pairs(data['leaderdata']) do
			if dataforstore['folder1'][name] then
				VariableType.new(name,dataforstore['folder1'][name],folder1)
			else
				VariableType.new(name,data['folder1'][name],folder1)
			end
		end
end)

Everything here has been dulled down exponentially to help understanding.

1 Like

it would be easier to help if we had all the related code (like the VariableType thing) and also the error message

No error message, and the variable type code is a flawless instance creation module:

local valueObject = {}

valueObject.types = {["string"] = "String", ["number"] = "Number", ["boolean"] = "Bool"}

function valueObject.new(name, value, parent)
	local valueType = valueObject.types[type(value)]
	if valueType then
		local newValue = Instance.new(valueType.."Value")
		newValue.Name = name
		newValue.Value = value
		newValue.Parent = parent
		return newValue
	end
end

return valueObject

The error lies in the fact that no matter what my script returns a value from the default dictionary, rather than the player’s own datastore.

can you try printing dataforstore? just to see what it’s storing, might make troubleshooting easier

1 Like

So apparently it’s the same as what is being returned. Im trying to reset my datastore with a seperate script and thats not working i guess

Players.PlayerRemoving:Connect(function(Player)
	local s, e = pcall(function()
		generaldatastore:SetAsync("uid-"..Player.UserId,data2)
        end)
end)

I JUST FIGURED OUT IM SETTING PLAYER DATA TO THE DEFAULT DATA

when you do :GetAsync(Player.UserId) you don’t add "uid-" at the start like when you do :SetAsync("uid-"..Player.UserId,data2) so you’re using the wrong key to get the player data

What I put up there was supposed to be an example and I insisted that everything that i didnt add was fine but I clearly missed something important. I’m very embarresed and i’m sorry.

If anybody wants, here is a simple script that helps you add new variables to your datastore. You can add variables and this script will automatically check for them and add them if they arent here. Once again, I’m very sorry for the mishap.

local data = {
	leaderdata = {
		Minutes = 0,
	},
}

Players.PlayerAdded:Connect(function(Player)
	local DataFromStore = nil

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local s, e = pcall(function()
		DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
	end)

	if s then
		print("Getting Data For: " .. Player.Name)
		local GetData = nil

		if DataFromStore then
			print("Data Loaded For: " .. Player.Name)
			GetData = DataFromStore
		else
			print("Created New Data For: " .. Player.Name)
			GetData = data
		end
		
		for name, _ in pairs(data['leaderdata']) do
            local variable  = Instance.new('NumberValue',leaderstats)
            variable.Name = name
			if GetData['leaderdata'][name] then
		        variable.Value = GetData['leaderdata'][name]
			else
				variable.Value = data['leaderdata'][name]
			end
		end
	else 
		warn("Error Getting Data For: " .. Player.Name)
	end
end)
1 Like