Data Store keeps on not working

I am making a save slot system for booths but the problem is that I keep on getting printed to me that the save isn’t working .

The thing that is wrong is whenever I try to save to the data store it prints to me that the save was unsuccessful

Yes I have API Enabled
If anyone has some ideas on what I am doing wrong cause I am somewhat new to datastore

Plate

outpit

local datastore = game:GetService("DataStoreService")
local Slots = datastore:GetDataStore("SaveSlot")
local player = script.Parent.Parent.Parent.Parent
local a = script.Parent

local tab = {
	One = {
	};
	Two = {
		
	};
	Three = {
		
	};
}
while true do
	local success, data = pcall(function()
		return Slots:GetAsync(player.UserId)
	end)
	if success then
		if not data then
			print("NEW PLAYER")
			Slots:SetAsync(player.UserId, tab)
		end
		break
	end
wait(2.5)
end

local function Update(data)
	print("SAVING")
	local slot = game.ServerStorage.Booths:FindFirstChild(player.Name)
	local booth = slot.Value.Booth
	print("saving to "..num)
	if num == "One" then
		NewTab = {
			One = {
				image = booth.Frame.Decal.Texture;
				BottomText = booth.Primary.Bottom.SurfaceGui.TextLabel.Text;
				Desc = booth.Desc.SurfaceGui.TextLabel.Text;
				PrimCol = booth.Primary:GetChildren()[1];
				SecCol = booth.Secondary:GetChildren()[1];
				TextCol =  booth.Desc.SurfaceGui.TextLabel.TextColor3;
				BottomFont = booth.Primary.Bottom.SurfaceGui.TextLabel.Font;
				DescFont = booth.Desc.SurfaceGui.TextLabel.Font;
			};
			data.Two;
			data.Three;	
		}
	end
	if num == "Two" then
		NewTab = {
			data.One;
			Two = {
				image = booth.Frame.Decal.Texture;
				BottomText = booth.Primary.Bottom.SurfaceGui.TextLabel.Text;
				Desc = booth.Desc.SurfaceGui.TextLabel.Text;
				PrimCol = booth.Primary:GetChildren()[1];
				SecCol = booth.Secondary:GetChildren()[1];
				TextCol =  booth.Desc.SurfaceGui.TextLabel.TextColor3;
				BottomFont = booth.Primary.Bottom.SurfaceGui.TextLabel.Font;
				DescFont = booth.Desc.SurfaceGui.TextLabel.Font;
			};
			data.Three;
		}
	end
	if num == "Three" then
		NewTab = {
			data.One;
			data.Two;
			Three = {
				image = booth.Frame.Decal.Texture;
				BottomText = booth.Primary.Bottom.SurfaceGui.TextLabel.Text;
				Desc = booth.Desc.SurfaceGui.TextLabel.Text;
				PrimCol = booth.Primary:GetChildren()[1].Color;
				SecCol = booth.Secondary:GetChildren()[1].Color;
				TextCol =  booth.Desc.SurfaceGui.TextLabel.TextColor3;
				BottomFont = booth.Primary.Bottom.SurfaceGui.TextLabel.Font;
				DescFont = booth.Desc.SurfaceGui.TextLabel.Font;
			};
		}
	end
	return NewTab
end

local function save()
	print("Somethings happening")
	local slot = game.ServerStorage.Booths:FindFirstChild(player.Name)
	if slot then -- checks if player owns booth
		print("this far")
		local success, data = pcall(function()
			return Slots:UpdateAsync(player.UserId, Update)
		end)
		if success then
			print("SAVE SUCESS")
		else
			print("SAVE FAILED AHHHHHHH")
		end
	end
end

local function load()
	local slot = game.ServerStorage.Booths:FindFirstChild(player.Name)
	if slot then -- checks if player owns booth
		local booth = slot.Value.Booth
		local success, data = pcall(function()
			return Slots:GetAsync(player.UserId)
		end)
		if success then
			if data then
				if data.One then
					print("Ah")
					if data.One.image then
						print("deded")
					end
				end
				local stuff = data[num]
				booth.Frame.Decal.Texture = stuff.image
				booth.Primary.Bottom.SurfaceGui.TextLabel.Text = stuff.BottomText
				booth.Desc.SurfaceGui.TextLabel.Text = stuff.Desc
				for _, prim in pairs(booth.Primary:GetChildren()) do
					prim.Color = stuff.PrimCol
				end
				for _, sec in pairs(booth.Secondary:GetChildren()) do
					sec.Color = stuff.SecCol
				end
				booth.Primary.Bottom.SurfaceGui.TextLabel.TextColor3 = stuff.TextCol
				booth.Desc.SurfaceGui.TextLabel.TextColor3 = stuff.TextCol
				booth.Primary.Bottom.SurfaceGui.TextLabel.Font = stuff.BottomFont
				booth.Desc.SurfaceGui.TextLabel.Font = stuff.DescFont
			end
		end
	end
end

for _, gah in pairs(script.Parent:GetDescendants()) do
	if gah.Name == "Save" then
		gah.MouseButton1Click:Connect(function()
			print("does this even work")
			num = gah.Parent.Name
			save()
		end)
	end
	if gah.Name == "Load" then
		gah.MouseButton1Click:Connect(function()
			num = gah.Parent.Name
			load()
		end)
	end
end
3 Likes

Try putting the while loop at the bottom of the script. It’s currently blocking the rest of the script.

1 Like

Nope didn’t work
I forgot to mention that my code is in a GUI would this be a problem
Also was I supposed to put something inside the while loop

Is allow API to access data stores on in the game settings?

Have u tried in a public server?, have u published the game? when i mean public server i mean in a live server not in studio.

I already said this in the post

Do a “try” system, basically something like this

local tries = 0
repeat
-- Data thing, checking success.
until tries == 3

And try using ordered data store for the slots.

Also yea I tried this in a public server
save failed duh

For the try system all the times it saved it said save failed also how would I incorporate an ordered data store

Try to remove the loop that saves data, to incorporate ordered data store its pretty easy, there is a tutorial in the API and you can simply change somethings there.

Wait I don’t understand it looks like Ordered Data Store is just for sorting data what would be the point of me using it

Are you saying that I should make a data store for every single one of the players save slots?

Yes, and only make it load the data when the player clicks to load, and add a cooldown between loading each data so they cant click all of the buttons at once and cause throttle.

Thanks for the reply I will be sure to try this out and Ill tell you if it works but quick question I still don’t understand why I would need an ordered data store because I only need access one at a time

Oh the reason I told you to do ordered data store is because you can simply do 3 different datas at once and save them all.

So then you can return these values and you do something like

local firstData = -- The pages table [1]
--[[Thing to get data]] = firstData.value

GUIs should be on the client and data store calls should only be called from the server and wont work from the client. Are you calling this from the client?

No they are in a normal script also I wasn’t having issues with it not working I was having issues with actual data being saved to data store

sorry I should of been more clear

You cannot store a dictionary in a dastastore.


In this case, you need to JSONEncode before saving, and JSONDecode whilst loading.

2 Likes