How to detect when the datastore is empty

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want a working datastore service
  2. What is the issue? Include screenshots / videos if possible!
    how do i detect that the datastore is empty
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local Players = game:GetService("Players")
local Frame = script.Parent.Frame
local ProductEvent  = game.ReplicatedStorage:WaitForChild("ProductEvents")
local ProductDatastore = game:GetService("DataStoreService"):GetDataStore("ProductList")
local Board = script.Parent:WaitForChild("Frame"):WaitForChild("Main"):WaitForChild("Board")
local Prompt = script.Parent.Parent:WaitForChild("Attachment"):WaitForChild("Button")

Prompt.Triggered:Connect(function(Plr)
	ProductEvent.Open:FireClient(Plr)
end)


--[[ ^^^ Ignore the function above ^^^ ]]--

ProductEvent:WaitForChild("AddAssetOnList").OnServerEvent:Connect(function(plr)
	if ProductDatastore ~= nil then
		-- do stuff here
	else
		-- do stuff here
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You can use :GetAsync() to get a players data. It returns nil if there isn’t any data stored in it
I suggest looking into the documentation of datastoreservice first to get a better understanding of it: DataStoreService | Roblox Creator Documentation

1 Like

Is this a solution, i just want to detect if there is NO keys on the datastore, but i tried your script (i didnt play test because i was busy making more stuff) but here is the script so far:

local Players = game:GetService("Players")
local Frame = script.Parent.Frame
local ProductEvent  = game.ReplicatedStorage:WaitForChild("ProductEvents")
local ProductDatastore = game:GetService("DataStoreService"):GetDataStore("ProductList")
local Board = script.Parent:WaitForChild("Frame"):WaitForChild("Main"):WaitForChild("Board")
local Prompt = script.Parent.Parent:WaitForChild("Attachment"):WaitForChild("Button")

Prompt.Triggered:Connect(function(Plr)
	ProductEvent.Open:FireClient(Plr)
end)

local function Queue(plr)
	local Succes,Error = pcall(function()
		ProductDatastore:GetAsync("Asset_"..plr.UserId)
	end)
	if Succes then
		return ProductDatastore:GetAsync("Asset_"..plr.UserId)
	else
		ProductEvent:WaitForChild("WarnPlayerErrorDS"):FireClient(plr)
		return nil
	end
end

ProductEvent:WaitForChild("SaveAssetOnList").OnServerEvent:Connect(function(plr, Asset)
	ProductDatastore:SetAsync("Asset_"..plr.UserId, Asset)
end)

ProductEvent:WaitForChild("AddAssetOnList").OnServerEvent:Connect(function(plr, Asset)
	local ReturnedQueue = Queue(plr)
	if ProductDatastore ~= nil then
		ProductEvent:WaitForChild("AddAssetDetecterCheck"):FireAllClients(Asset)
	else
		ProductEvent:WaitForChild("WarnPlayerErrorDS"):FireClient(plr)
	end
end)

Ignore the function name “Queue” Its just a terrible name that came to my head

Other than a few misconception, lgtm.

-- ... --

local function Queue(plr)
	-- The use of pcall is to protect it from error;
	local Succes, Data = pcall(function()
		ProductDatastore:GetAsync("Asset_"..plr.UserId)
	end)

	--[=[
	if Succes then
		return ProductDatastore:GetAsync("Asset_"..plr.UserId)
	else
		ProductEvent:WaitForChild("WarnPlayerErrorDS"):FireClient(plr)
		return nil
	end]=]

	return Data
end

ProductEvent:WaitForChild("SaveAssetOnList").OnServerEvent:Connect(function(plr, Asset)
	--[[
		I advice that you add a debounce for this or a condition;
		is the remote event being spammed etc. if so then you could
		do something with that player, like kick/warn the player.
	]]
	ProductDatastore:SetAsync("Asset_"..plr.UserId, Asset)
end)

ProductEvent:WaitForChild("AddAssetOnList").OnServerEvent:Connect(function(plr, Asset)
	local Data = Queue(plr)
	--[[
	if ProductDatastore ~= nil then
		ProductEvent:WaitForChild("AddAssetDetecterCheck"):FireAllClients(Asset)
	else
		ProductEvent:WaitForChild("WarnPlayerErrorDS"):FireClient(plr)
	end]]

	if Data then
		ProductEvent:WaitForChild("AddAssetDetecterCheck"):FireAllClients(Asset)
	else
		ProductEvent:WaitForChild("WarnPlayerErrorDS"):FireClient(plr)
	end
end)