Can data stores not be read in studio? [URGENT]

I am trying to read all keys of a data store and I think I’ve done everything correctly, but it reads the keys as nil. Here’s the script:

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local addGui = ReplicatedStorage.FireAddGUI

local DataStoreService = game:GetService("DataStoreService")
local rankedStore = DataStoreService:GetDataStore("RankedStore")

Players.PlayerAdded:Connect(function(plr)
	if plr:GetRankInGroup(11635716) >= 254 then
		local pages = rankedStore:ListKeysAsync()
		
		local passed = {}
		
		while task.wait(0.1) do		
			for i, v in ipairs(pages:GetCurrentPage()) do
				table.insert(passed,v)
			end
			
			if pages.IsFinished == true then
				break
			end
			
			pages:AdvanceToNextPageAsync()
		end	
		
		for i, name in pairs(passed) do
			addGui:FireClient(plr,name.KeyName)
			print("ui request sent")
		end
	end
end)

How can I fix this?

Please be serious. This is not a place for jokes.

Make sure API Service Access is granted to your experience in Studio.
File → Game Settings → Security → Enable Studio Access to API Services

Yes, API Service Access is granted.

I might be completely reading this wrong, but are you ever setting the keys? As far as I can tell its not showing you setting keys in rankedStore, but correct me if im wrong

Yes. Here is the script that sets and removes keys:

local DataStoreService = game:GetService("DataStoreService")
local RankedStore = DataStoreService:GetDataStore("RankedStore")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local addPlayer = ReplicatedStorage.FireAddPlayer
local removePlayer = ReplicatedStorage.FireRemovePlayer

addPlayer.OnServerEvent:Connect(function(player)
	local success, errorMessage = pcall(function()
		RankedStore:SetAsync(player, "passed")
	end)
	if not success then
		print(errorMessage)
	end
	if success then
		print("player added")
	end
end)

removePlayer.OnServerEvent:Connect(function(plr, name)
	local success, removedPlayer = pcall(function()
		RankedStore:RemoveAsync(name)
	end)
	if success then
		print(removePlayer)
	end
end)

Sorry for the wait, went out with my family

But if the issue was on saving data, then this should solve your issues, if it was not about the saving, please clarify for me and ill try my best!

local DataStoreService = game:GetService("DataStoreService")
local RankedStore = DataStoreService:GetDataStore("RankedDataStore")

--[[
got rid of the remote events, just use the built in functions its way easier

changed to different async events 

changed the key to Player_[their user ID]
]]

game.Players.PlayerAdded:Connect(function(player)
	--load data
	local success, errorMessage = pcall(function()
		RankedStore:GetAsync("Player_".. player.UserId) -- changed to getasync
	end)
	if not success then
		print(errorMessage)
		warn("There was an issue loaded ".. player.Name.. "'s data")
	end
	if success then
		print("Loaded ".. player.Name.. "'s data")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	--save data
	local data = {}
	--set "data" to whatever data you need to set, load it through whatever
	
	local success, errorMessage = pcall(function()
		RankedStore:SetAsync("Player_".. plr.UserId, data) -- changed to set async
	end)
	if success then
		print("Saved ".. plr.Name.. "'s data")
	else
		print(errorMessage)
		warn("There was an issue saving ".. plr.Name.. "'s data")
	end
end)
1 Like

The problem is with reading the data store. It should have my username as a key, but when I read it, it comes out nil. I want to read all keys, not just one. I tried changing my code to this:

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local addGui = ReplicatedStorage.FireAddGUI

local DataStoreService = game:GetService("DataStoreService")
local rankedStore = DataStoreService:GetDataStore("RankedStore")

Players.PlayerAdded:Connect(function(plr)
	if plr:GetRankInGroup(11635716) >= 254 then
		local success, pages = pcall(function()
			return rankedStore:ListKeysAsync()
		end)
		
		if success then
			local passed = {}

			while task.wait(0.1) do		
				for i, v in ipairs(pages:GetCurrentPage()) do
					table.insert(passed,v)
				end

				if pages.IsFinished == true then
					break
				end

				pages:AdvanceToNextPageAsync()
			end	

			for i, name in pairs(passed) do
				addGui:FireClient(plr,name.KeyName)
				print("ui request sent")
			end
		end
	end
end)

because I thought I needed to do a pcall, but it didn’t work.

Actually, the problem could be with setting data. I tried changing the SetAsync(player, “passed”) to SetAsync(player.Name, “passed”), but it still says nil. Do you have any other ideas?

I did some debugging, and I found out that it is setting the keys correctly, but it’s having trouble with name.KeyName, do you know how to fix this?

Loop through passed and print each index, and tell us what it prints

Ok, so there are 2 items in passed, so it printed 62penguins and Instance. For some reason, they come out nil when it puts it in the text label.

:man_facepalming: I found the issue.

Ok, so what I was doing was I was going through the keys and sending a request to build a gui for each one to a certain player in this script:

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local addGui = ReplicatedStorage.FireAddGUI

local DataStoreService = game:GetService("DataStoreService")
local rankedStore = DataStoreService:GetDataStore("RankedStore")

Players.PlayerAdded:Connect(function(plr)
	if plr:GetRankInGroup(11635716) >= 254 then
		local success, pages = pcall(function()
			return rankedStore:ListKeysAsync()
		end)
		
		if success then
			local passed = {}

			while task.wait(0.1) do		
				for i, v in pairs(pages:GetCurrentPage()) do
					table.insert(passed,v)
				end

				if pages.IsFinished == true then
					break
				end

				pages:AdvanceToNextPageAsync()
			end	

			for i, name in pairs(passed) do
				addGui:FireClient(plr,name.KeyName)
				print("ui request sent for "..name.KeyName)
			end
		end
	end
end)

Now, since usually when I work with remote events, I’m firing it from the client, not the server. When you fire it from the client, the server receives the client it was fired from and any other arguments you send through.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local fireAddGui = ReplicatedStorage.FireAddGUI

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local playerGui = localPlayer:WaitForChild("PlayerGui")
local resultsScreen = playerGui:WaitForChild("ResultsScreen")
local resultsFrame = resultsScreen.ScrollingFrame
local playerEx = resultsFrame.PlayerExample

local xValue = playerEx.Position.X.Offset
local yValue = playerEx.Position.Y.Offset

fireAddGui.OnClientEvent:Connect(function(plr,name)
	local player = playerEx:Clone()
	player.Parent = resultsFrame
	player.Visible = true
	player.Position = UDim2.new(0, tonumber(xValue), 0, tonumber(yValue) + 60)
	player.Text = tostring(name)
	print("ui added")
	
	playerEx = player
	xValue = player.Position.X.Offset
	yValue = player.Position.Y.Offset
end)

So if this script were a server side script and the first one was client side, plr would be me, the player it was sent from and name would be the keyname. Since this one is client and the first one is server, plr is irrelevant, because when a remote event is fired from the server, the client receives only the arguments passed through. I was mixing those up, so I simply got rid of the plr parameter, and the issue was fixed! :smile:

P.S.
This response is mostly for people who look at this topic later.