Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why can't i access this DataStore?

I'm trying to save some stats but can't understand why the player cannot access the DataStore, It does not succeed on line 38 why?

Serverscript:

local RepStorage = game:GetService("ReplicatedStorage")
local SerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")

local sessionData = {}
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("Data1")

--function PlayerStatManager:ChangeData(player, statName, changeValue)
--	sessionData[player][statName] = changeValue
--end
--
--function PlayerStatManager:AddData(player, statName, changeValue)
--	sessionData[player][statName] = sessionData[player][statName] + changeValue
--end
--
--function PlayerStatManager:InsertData(player, statName, insertValue)
--	table.insert(sessionData[player][statName], insertValue)
--end
--
--function PlayerStatManager:GetData(player)
--	return sessionData[player]
--end

function dataStoreRetry(dataStoreFunction)
	local tries = 0
	local success = true
	local data = nil
	repeat
		tries = tries + 1
		success = pcall(function() data = dataStoreFunction() end)
		if not success then
			wait(1)
		end
	until tries == 3 or success
	if not success then
		error("Could not access DataStore!")
	end
	return success, data
end

function getPlayerData(player)
	return dataStoreRetry(function()
		return playerData:GetAsync(player.UserId)
	end)
end

function savePlayerData(player)
	if sessionData[player] then
		return dataStoreRetry(function()
			return playerData:SetAsync(player.UserId, sessionData[player])
		end)
	end
end

game:GetService("Players").PlayerAdded:Connect(function(player)
	local success, data = getPlayerData(player)
	
	if not success then
		error("Could not access DataStore!")
		sessionData[player.UserId] = false
	else
		if not data then
			sessionData[player.UserId] = {
				["Gold"] = 0,			
				["Silver"] = 0,
				
				["Inventory"] = {},			
				
				["Main"] = "",	
				["Secondary"] = "",	
				
			}
			savePlayerData(player)
		else
			sessionData[player] = data
		end
	end
	
end)

(error) / warning message:

22:21:03.131 - ServerScriptService.Script:38: Could not access DataStore!
22:21:03.131 - Stack Begin
22:21:03.133 - Script 'ServerScriptService.Script', Line 38 - global dataStoreRetry
22:21:03.133 - Script 'ServerScriptService.Script', Line 44 - global getPlayerData
22:21:03.134 - Script 'ServerScriptService.Script', Line 58
22:21:03.134 - Stack End

3 comments

0

What is dataStoreFunction()

0

its a function passed as an argument to dataStoreRetry, containing the actual command like "return playerData:GetAsync(player.UserId)"

0

by calling it with () as in dataStoreFunction()`you execute the function and the return value is in this case passed to the variable data

1 answer

✓ Accepted answer

Is it not because you are running this in studio (while 'datastore available in studio' is turned off for your game/you didnt publish)?

This discussion is preserved as it was. New answers, comments, and votes are closed.