Data store comes out nil after storing data

I am saving some data in a data store for my anti exploit system, but whenever I store some data under a certain key, when I get that data, it says that it is nil.

I have done tons of testing, with print and warn statements, trying different values and keys, but I still am getting nil

This is actually my second version of this code, and it is pretty complex (interacting with other scripts), as well as not being finished, so I am only including the relevant parts of it.

local DataStoreService = game:GetService("DataStoreService")

local hackerData = DataStoreService:GetDataStore("hackerData")

local function tableToString(table1)     #all this does is convert the tables from looking like this: 'table:9&b569c840r' to this: 'Table: test1, test2, test3'
	if type(table1) == "table" then
		returnedData = "Table: " .. table1[1]

		for count = 2, #table1 do
			returnedData = returnedData .. ", " .. tostring(table1[count])
		end
	else
		returnedData = table1
	end

	return(returnedData)
end

local function setData(key, data)
	local setSuccess, errorMessage = pcall(function()
		hackerData:SetAsync(key, data)
	end)
	if not setSuccess then
		if errorMessage then
			warn(errorMessage)
		end

		warn("Couldn't add '", tableToString(data), "' to 'hackerData' under the key: '", key, "'")
	else
		warn("Added: '", tableToString(data), "' to 'hackerData' under the key: '", key, "'")
	end
end

local function getData(key)
	local getSuccess, data = pcall(function()
		hackerData:GetAsync(key)
	end)
	if getSuccess then
		warn("Got '", tableToString(data), "' from 'hackerData' under the key: '", key, "'")
	else
		warn("Couldn't get data from 'hackerData' under the key: '", key, "'")
	end
	
	return data
end

setData("HackDates", {"TestDate1", "TestDate2"})

function check(player, tpPlaceID)
	warn("Server received teleport event")
	warn("")
		
	ogHackDates = getData("HackDates")
		
	print(ogHackDates)

end

teleportSendEvent.OnServerEvent:Connect(check)

Here is what the output says:

The stuff I colored over is output from other parts of my code that i did not include here

The second variable in a pcall function will be what is returned, so to store the data under that variable you have to return what you get from the datastore.

local getSuccess, data = pcall(function()
 return hackerData:GetAsync(key)
end)

I had that at first, but it wasnt working. I will try it again though.

EDIT: Ohhh i think that i wasnt using the second variable when i did have the return in there

1 Like

I don’t know so much about DataStoreService or DataStoreService2

But the script that put the data in the Value is a script or local script? I think that Local Script can’t put Values and the DataStore see them.

It’s all in one server script.

1 Like

Only server scripts (& Modules required by a server script) can access DataStores.

Did that fix ur problem?


Yes I marked it as solution thank you