Is there something wrong with my datastore?

I have written this script over and over and over again and I dont see whats wrong, I have also gone through the page on developer.roblox.com. What am I missing here?

local DataStoreService = game:GetService("DataStoreService")
local InventoryDataStore = DataStoreService:GetDataStore("InventoryStore")

game.Players.PlayerAdded:Connect(function(Player)
	local inventory = Instance.new("Folder")
	inventory.Name = "Inventory"
	inventory.Parent = Player
	
	local slot1 = Instance.new("StringValue")
	slot1.Name = "Slot1"
	slot1.Parent = inventory
	slot1.Value = "nil"
	
	local InventoryStore
	local success, err = pcall(function()
		InventoryStore = InventoryDataStore:GetAsync(Player.UserId.."-slot1")
	end)
	
	if success then
			print("Successfully Loaded Inventory Data")
			slot1.Value = InventoryStore
		else
			print("Inventory Data Not loaded Correctly")
		end
	
	if slot1.Value == "nil0" then
		slot1.Value = "nil"
	end
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local success, err = pcall(function()
		InventoryDataStore:SetAsync(Player.UserId.."-slot1",Player.Inventory.Slot1.Value)
	end)
	
	if success then
		print("Inventory Data Successfully Saved")
	else
		print("Inventory Data Not Saved Correctly")
	end
end)

I get the:

502: API Services rejected request with error. HTTP 403 (Forbidden)

Studio API Is allowed on the game.

1 Like

DataStoreService is defined at the start of the script. local DataStoreService = game:GetService("DataStoreService").

It is, sorry I forgot to hit return after the code block. It was always there, so thats not the problem. Should I change it to:

game:GetService("DataStoreService"):GetDataStore("InventoryStore")

So its one thing instead of two?

Like I said I forgot to hit return after the code block, my apologies.

You can find the error code by replacing

print("Inventory Data Not loaded Correctly")

with

warn(err)
-- or
print(err)

This will give the error message and make it easier to debug the code.

If you accept a suggestion from a beginner who suffered a lot using the Roblox native DataStore, I give you the solution: forget about DataStore and use DataStore2.

1 Like

I got:

502: API Services rejected request with error. HTTP 403 (Forbidden)

Studio does have the API allowed.

If API Services are enabled but it is giving a 502 error with HTTP 403, that would mean that your are in team create and you are not the owner, in studio, you can only use data stores if you are the owner, if you aren’t the owner, roblox servers reject the request saying that you are not allowed to access it. Try testing it on the roblox application instead.

I am not in team create, and I am the one who created the game. The game doesnt have TC on.

Are you sure you have API Services enabled? You can see it in Game Settings under the Options tab.

1 Like

Yes im 100% sure.

And here you can see that TC is off.

It is weird if API Services are enabled but it is giving you that error, there is probably nothing wrong with the code and something wrong with the game settings, try to test it on the roblox application and see what happens.

I did that and it worked just fine saving my data on the client, no errors. Any other ideas on why its just not working in studio? Im going to restart my laptop right now and see if it has anything to do with that.

Update: The restart did nothing.

I have no idea why it’s not working in studio, I tried it and it worked for me just fine.

It has happened to me when it does not work in studio, maybe it is a roblox bug?

Try it on different game place. Is it still error?

Tried on a different place and getting the same error.

Has it happened to you recently? If it has, then I might as well report it as a bug to Roblox.

Yes it has! I was working on the obby, got no error but data save did not work in roblox studio.