Turning a nil value of a datastore to 0

Hey there!

So here's what i'm trying to do

When a player presses a button it’ll pop up a loading screen, That loading screen is trying to load in a numbervalue datastore to check something about it. If it’s 0: Pull up a screen to purchase and add to the number, If its 1 - 10: Pull up a screen confirming that you want to use that point.

What I've tried
local api = workspace.quick.QUICKAPI
local dss = game:GetService("DataStoreService")
local flightsStore = dss:GetDataStore("quickFlightsAvalible")
api.OnServerEvent:Connect(function(player,data)
local success, avalible pcall(function()
	return flightsStore:GetAsync("player_"..player.UserId)
end)
 if avalible == nil then
	local success, err pcall(function()
		flightsStore:SetAsync("player_"..player.UserId, 0)
	end)
end
local success, avalible2 pcall(function()
	return flightsStore:GetAsync("player_"..player.UserId)
end)
api:FireClient(player,{"GET_DATASTORE",avalible2})
    end)

There are no errors for this however it continues to print nil

Solutions I've tried

Changing the value to “NONE”

Hey!

Is this intentional?

local success, avalible pcall(function()
	return flightsStore:GetAsync("player_"..player.UserId)
end)

You seem to not have any = in that code, which means that code errors.

I’m not sure why your code wouldn’t work besides that.

Here’s the fix for you:

local api = workspace.quick.QUICKAPI
local dss = game:GetService("DataStoreService")
local flightsStore = dss:GetDataStore("quickFlightsAvalible")
api.OnServerEvent:Connect(function(player,data)
local success, avalible = pcall(function()
	return flightsStore:GetAsync("player_"..player.UserId)
end)
 if avalible == nil then
	local success, err = pcall(function()
		flightsStore:SetAsync("player_"..player.UserId, 0)
	end)
end
local success, avalible2 = pcall(function()
	return flightsStore:GetAsync("player_"..player.UserId)
end)
api:FireClient(player,{"GET_DATASTORE",avalible2})
end)

Also, note that you don’t need to be using pcall for this. If the key doesn’t exist, it will simply return nil.

This means you can replace the first three lines of the event firing with a simple local avalible = flightsStore:GetAsync("player_"..player.UserId)

Regardless, please let me know if adding the = fixes your problem.

2 Likes

Like @MetatableIndex mentioned, your code might error due to the fact that you did not put the equal signs (=). If @MetatableIndex’s code does not work, then you can try using this one:

local api = workspace:WaitForChild("quick"):WaitForChild("QUICKAPI")
local dss = game:GetService("DataStoreService")
local flightsStore = dss:GetDataStore("quickFlightsAvalible")
api.OnServerEvent:connect(function(player, data)
	local storedData
	local success, errorMessage = pcall(function()
		storedData = flightsStore:GetAsync("player_"..player.UserId)
	end)
	if success and not storedData then
		local success2, errorMessage2 = pcall(function()
			flightsStore:SetAsync("player_"..player.UserId, 0)
		end)
		if not success2 and errorMessage2 then
			print("Error setting data: "..errorMessage2)
		end
	elseif not success and errorMessage then
		print("Error getting data: "..errorMessage)
	end
	local newStoredData
	local success3, errorMessage3 = pcall(function()
		storedData = flightsStore:GetAsync("player_"..player.UserId)
	end)
	if success3 and newStoredData then
		api:FireClient(player, {"GET_DATASTORE", newStoredData})
	elseif not success3 and errorMessage then
		print("Error getting updated data: "..errorMessage3)
	end
end)

Just want to add something on here: You might wanna add some sort of tick to make sure they cannot send too many requests to the data-store which could end in a disaster.

1 Like

Yeah, that is something I do on my own code. The code above is just a simple remake based on the original code’s style. If I were to write the code the way I typically code, then I would add way more stuff to it. This is just a simple edit of the original one that works in case the one that MetatableIndex does not work. Thank you for the notice thought.

1 Like

It’s a 1 time event that can’t be sent more than once in a sesson

Alright so that code might work because NEW ISSUE!
My API For actually sending a request for data will just not respond.

local api = workspace:WaitForChild("quick"):WaitForChild("QUICKAPI")
 script.Parent.Changed:Connect(function()
	if script.Parent.Value == true then
		print("Getting your store.")
	api:FireServer({"GET_DATASTORES"})
	end
   end)

This just won’t give it back,

api.OnClientEvent:Connect(function(data)
	if data[1] == "SEND_DATASTORE" then
		print("Reciving store, Please wait.")
		wait(2)
		print("Collected store, Checking value.")
		script.Parent.Value = false
		if data[2] == 0 then
			 script.Parent.Parent.Parent.Parent.purchaseFrame:TweenPosition(UDim2.new(0.5,0,0.5,0),"InOut","Quad",0.2)
		end
	end
end)

This won’t respond with it receiving the datastore. Could it be that it can’t send datastores over the client server boundary? This kind of thing is new to me soooo yea.

local api = workspace:WaitForChild("quick"):WaitForChild("QUICKAPI")
local dss = game:GetService("DataStoreService")
local flightsStore = dss:GetDataStore("quickFlightsAvalible")
api.OnServerEvent:connect(function(player, data)
	if data[1] == "GET_DATASTORES" then
	local storedData
	local success, errorMessage = pcall(function()
		storedData = flightsStore:GetAsync("player_"..player.UserId)
	end)
	if success and not storedData then
		local success2, errorMessage2 = pcall(function()
			flightsStore:SetAsync("player_"..player.UserId, 0)
		end)
		if not success2 and errorMessage2 then
			print("Error setting data: "..errorMessage2)
		end
	elseif not success and errorMessage then
		print("Error getting data: "..errorMessage)
	end
	local newStoredData
	local success3, errorMessage3 = pcall(function()
		storedData = flightsStore:GetAsync("player_"..player.UserId)
	end)
	if success3 and newStoredData then
		api:FireClient(player, {"SEND_DATASTORE", newStoredData})
	elseif not success3 and errorMessage then
		print("Error getting updated data: "..errorMessage3)
	end
	end
end)

I also made a check for this to make sure it’s the right api request.
Edit: It’s not getting past the first part where it gets the data, Not even a error.

Just another heads up, you should never ever trust the client with your data, and just want to confirm, how is this used?

it needs to check how many “flights” it has in the account, If it’s 0 ask the user to purchase some more. If it’s above 0 then ask them if they want to use that flight. The data is for that player only.

Alright i’m back!
So with the help of @MetatableIndex with that piece of code and a few fixes here’s a working version

Handler
 local api = workspace:WaitForChild("quick"):WaitForChild("QUICK_API")
    local dss = game:GetService("DataStoreService")
    local flightsStore = dss:GetDataStore("quickFlightsAvalible")
    api.OnServerEvent:Connect(function(player, data)
    	wait(1)

    	if data[1] == "GET_DATASTORES" then
    			print("User:"..player.Name.." wants a datastore")
    	local storedData
    	local success, errorMessage = pcall(function()
    		print("Collecting")
    		storedData = flightsStore:GetAsync("player_"..player.UserId)
    		print(storedData)
    	end)
    	if success and not storedData then
    		print("Step 1: Success")
    		local success2, errorMessage2 = pcall(function()
    			print("Setting")
    			flightsStore:SetAsync("player_"..player.UserId, 0)
    		end)
    		if not success2 and errorMessage2 then
    			print("Error setting data: "..errorMessage2)
    		end
    	elseif not success and errorMessage then
    		print("Error getting data: "..errorMessage)
    	end
    	local newStoredData
    	local success3, errorMessage3 = pcall(function()
    		print("Rechecking")
    		storedData = flightsStore:GetAsync("player_"..player.UserId)
    	end)
    	if success3 and storedData then
    		print("Step 2: Success, Firing.")
    		print("Sending")
    		api:FireClient(player, {"GIVE_DATASTORE", storedData})
    	elseif not success3 and errorMessage then
    		print("Error getting updated data: "..errorMessage3)
    	end
    		end
    end) 
Requester / Reciver
local api = workspace.quick.QUICK_API
script.Parent.Changed:Connect(function()
	if script.Parent.Value == true then
	api:FireServer({"GET_DATASTORES"})
	print("Getting your store.")
	end
end)


api.OnClientEvent:Connect(function(data)
		print("Reciving event, Please wait.")
	if data[1] == "GIVE_DATASTORE" then
		print("Detected correct event, Please wait.")
		wait(2)
		print("Collected store, Checking value.")
		print("Value: "..data[2])
		local flightsaval = tonumber(data[2])
		if data[2] == flightsaval then
			script.Parent.Value = false
			script.Parent.Parent.Parent.Parent.purchaseFrame:TweenPosition(UDim2.new(0.5,0,0.5,0),"InOut","Quad",0.2)
		end
	end
end)

Thanks for the help!