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

Datastore cannot save other players stats why?

I'm trying to learn some datastore of the roblox wiki but when i wrote the code they had and tried to understand it it worked fine for me. But when why friends joins the server i get an error and i get the message the pcall got me.

Why is this happening, why is it working for the datastore to save the stats for me but not my friends or any other players?

Dictionary cannot be stored in DataStore

DataStore could not be accessed! Data might not save.

Script:

local module = {}

local ToolModule = require(game.ReplicatedStorage.Classes.Modules.Tools)

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData7")

local AUTOSAVE_INTERVAL = 30

local DATASTORE_RETRIES = 3

local PlayerData = {}

function module:ChangeStat(player, statName, changeValue)
	PlayerData[player][statName] = changeValue
end

function module:AddValue(player, statName, changeValue)
	PlayerData[player][statName] = PlayerData[player][statName] + changeValue
end

function module:ChangeTable(player, statName, item, changeValue)
	PlayerData[player][statName][1][item] = changeValue
end

function module:ReturnStats(player)
	return PlayerData
end

local function DataStoreRetry(DataStoreFunction)
	local tries = 0
	local success = true
	local data = nil
	repeat
		tries = tries + 1
		success, message = pcall(function() data = DataStoreFunction() end)
		print(data)
		if not success then wait(1) end
	until tries == DATASTORE_RETRIES or success
	if not success then
		print(message)
		error("DataStore could not be accessed! Data might not save.")
	end
	return success, data
end

function module:GetStats(player)
	return DataStoreRetry(function()
		return playerData:GetAsync(player.UserId)
	end)
end

local function savePlayerData(player)
	if PlayerData[player] then
		return DataStoreRetry(function()
			return playerData:SetAsync(player.UserId, PlayerData[player])
		end)
	end
end

function LoadData(player)
	return DataStoreRetry(function()
		return playerData:GetAsync(player.UserId)
	end)
end

function SetupPlayerData(player)		
	local success, data = LoadData(player)
	if not success then
		PlayerData[player] = false
	else
		if not data then
			print("Save not found, creating a new save!")
			PlayerData[player] = {
				Cash = 0,
				Level = 1,
				Experience = 0,
				Rebirth = 0,
				Rank = "Beginner",
				Tool = "GravityGun",
				Backpack = "Backpack1",
				BackpackAmount = 0,
				ToolStats = {
					ToolModule,
				}
			}
			savePlayerData(player)
		else
			PlayerData[player] = data
		end
	end
end

function AutoSave()
	while wait(AUTOSAVE_INTERVAL) do
		for player, data in pairs(PlayerData) do
			print("Saving Data!")
			savePlayerData(player)
		end
	end
end

spawn(AutoSave)

game.Players.PlayerAdded:Connect(SetupPlayerData)

game.Players.PlayerRemoving:Connect(function(player)
	savePlayerData(player)
	
	PlayerData[player] = nil
end)

return module

2 comments

0

Do you get any errors?

0

No, no errors only the message that says "Dictonary cannot be stored in Datastore"

1 answer

✓ Accepted answer

Okay so I checked the script and saw the actual error was this where you coded print(message) that prints the actual error.

502: API Services rejected request with error: HTTP 0 (HTTP 403 (HTTP/1.1 403 Forbidden))

I looked and wiki and saw that you should go on your account on roblox website then click on "Create" it was "Develop" then go the your game then click on configure game and then 'Toggle the Enable Studio access to Api services' setting in Basic Settings tab.

I read that here scroll to the 403 error. https://wiki.roblox.com/index.php?title=Datastore_Errors

2 comments

0

Let me know if that solved the problem.

0

Thanks!

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