Why does my save not work?

basically, i want to save some properties of some gui objects, but it doesnt save and not print anything this is my script:

-- // services
local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
local dataStore = dataStoreService:GetDataStore("Gui")

-- // variables
local keyPrefix = "Player: "
local guisToSave = {
    WatterBottle = game.StarterGui.Upgrades.Main.MainUpgrades.WatterBottle,
    PC = game.StarterGui.Upgrades.Main.MainUpgrades.PC,
    Hut = game.StarterGui.Upgrades.Main.MainUpgrades.Hut,
    Food = game.StarterGui.Upgrades.Main.MainUpgrades.Food,
    Upgr1 = game.StarterGui.Upgrades.Main.PrestiegeUpgrades.Upgr1,
    Upgr2 = game.StarterGui.Upgrades.Main.PrestiegeUpgrades.Upgr2,
    Upgr3 = game.StarterGui.Upgrades.Main.PrestiegeUpgrades.Upgr3,
    PrestiegeText = game.StarterGui.Upgrades.Main.Prestiege
}

-- // functions

local function save(player: Player)
    local key = keyPrefix .. tostring(player.UserId)
    local data = {}

    for guiName, gui in pairs(guisToSave) do
        data[guiName] = {
            Visible = gui.Visible,
            Text = gui.Text
            -- Add more properties as needed
        }
    end

    local success, err = pcall(function()
        dataStore:UpdateAsync(key, function()
            return data
        end)
    end)

    if not success then
        warn("Failed to save data: " .. tostring(err))
    end
end

local function load(player: Player)
    local key = keyPrefix .. tostring(player.UserId)

    local success, err
    local data
    repeat
        success, err = pcall(function()
            data = dataStore:GetAsync(key)
        end)

    until success or not player:FindFirstChild(player.Name)

    if not data then return end

    if success then
        for guiName, properties in pairs(data) do
            local gui = guisToSave[guiName]
            if gui then
                gui.Visible = properties.Visible
                gui.Text = properties.Text
                -- Apply more properties as needed
            end
        end
    else
        warn("Failed to load data: " .. tostring(err))
    end
end

-- // signals
players.PlayerAdded:Connect(save)
players.PlayerRemoving:Connect(load)

game:BindToClose(function()
    for i, plr: Player in ipairs(players:GetPlayers()) do
        save(plr)
    end
end)

like i said, i want to save the visibility of the gui and the text, and load them once we rejoin.

So the first thing i noticed is when the player is leaving you’re loading and when the player is joining you’re saving so I swapped those around. Also I’m not the best with datastores as I haven’t worked with them in a while but I remember needing to create a datastore data thingy with :SetAsync() I believe so I added that here to create it if the player doesn’t have a datastore lmk if it doesn’t work and I’ll try to see what else could be the problem. Apologies in advance if I’m wrong

local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
local dataStore = dataStoreService:GetDataStore("Gui")


-- // variables
local keyPrefix = "Player: "
local guisToSave = {
	WatterBottle = game.StarterGui.Upgrades.Main.MainUpgrades.WatterBottle,
	PC = game.StarterGui.Upgrades.Main.MainUpgrades.PC,
	Hut = game.StarterGui.Upgrades.Main.MainUpgrades.Hut,
	Food = game.StarterGui.Upgrades.Main.MainUpgrades.Food,
	Upgr1 = game.StarterGui.Upgrades.Main.PrestiegeUpgrades.Upgr1,
	Upgr2 = game.StarterGui.Upgrades.Main.PrestiegeUpgrades.Upgr2,
	Upgr3 = game.StarterGui.Upgrades.Main.PrestiegeUpgrades.Upgr3,
	PrestiegeText = game.StarterGui.Upgrades.Main.Prestiege
}



-- // functions

local function save(player: Player)
	local key = keyPrefix .. tostring(player.UserId)
	local data = {}

	for guiName, gui in pairs(guisToSave) do
		data[guiName] = {
			Visible = gui.Visible,
			Text = gui.Text
			-- Add more properties as needed
		}
	end
	
	--checks if player has a datastore if not it will create one
	local success, err = pcall(function()
		if not dataStore:GetAsync(key) then
			print("Player doesn't have a datastore!")
			dataStore:SetAsync(key, data)
		end
	end)
	
	local success, err = pcall(function()
		dataStore:UpdateAsync(key, function()
			return data
		end)
	end)

	if not success then
		warn("Failed to save data: " .. tostring(err))
	end
end

local function load(player: Player)
	local key = keyPrefix .. tostring(player.UserId)

	local success, err
	local data
	repeat
		success, err = pcall(function()
			data = dataStore:GetAsync(key)
		end)

	until success or not player:FindFirstChild(player.Name)

	if not data then return end

	if success then
		for guiName, properties in pairs(data) do
			local gui = guisToSave[guiName]
			if gui then
				gui.Visible = properties.Visible
				gui.Text = properties.Text
				-- Apply more properties as needed
			end
		end
	else
		warn("Failed to load data: " .. tostring(err))
	end
end

-- // signals
players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)

game:BindToClose(function()
	for i, plr: Player in ipairs(players:GetPlayers()) do
		save(plr)
	end
end)
1 Like

It gave me a error and the error is:

19:26:37.834 DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = Player: 343744101 - Studio

Did it occur when you left the game or when you joined the game?

When i left, my studio froze, and waiting in shock it unfroze and the thing gave me that error (the output)

Try making a local server and see if it happens when only one of the players leaves, if it doesn’t then it may be due to the loop down here going through the players too fast

game:BindToClose(function()
	for i, plr: Player in ipairs(players:GetPlayers()) do
		save(plr)
	end
end)

Its a singleplayer game with max 1 player per server, im gonna start a local server now

If the server closes anyways due to it being one player max then try this in the for loop

game:BindToClose(function()
	for i, plr: Player in ipairs(players:GetPlayers()) do
		save(plr)
		wait(1)
	end
end)

oh and i’d add a maximum retries in your load function just incase someone doesn’t have any data it doesn’t spam roblox for eternity as well as slowing the requests down in it

Wait, i see a glimpse of hope, i think it works wait as im still testing

oh quick question did it ever print that the player didn’t have a datastore?

I have no idea, but i can check

if it didn’t it might be the check and now that I’m thinking about it the for loop doesn’t have any reason to send it more than once but i’d just add some print statements throughout to see if something prints a million times

local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
local dataStore = dataStoreService:GetDataStore("Gui")


-- // variables
local keyPrefix = "Player: "
local guisToSave = {
	WatterBottle = game.StarterGui.Upgrades.Main.MainUpgrades.WatterBottle,
	PC = game.StarterGui.Upgrades.Main.MainUpgrades.PC,
	Hut = game.StarterGui.Upgrades.Main.MainUpgrades.Hut,
	Food = game.StarterGui.Upgrades.Main.MainUpgrades.Food,
	Upgr1 = game.StarterGui.Upgrades.Main.PrestiegeUpgrades.Upgr1,
	Upgr2 = game.StarterGui.Upgrades.Main.PrestiegeUpgrades.Upgr2,
	Upgr3 = game.StarterGui.Upgrades.Main.PrestiegeUpgrades.Upgr3,
	PrestiegeText = game.StarterGui.Upgrades.Main.Prestiege
}



-- // functions

local function save(player: Player)
	local key = keyPrefix .. tostring(player.UserId)
	local data = {}

	for guiName, gui in pairs(guisToSave) do
		data[guiName] = {
			Visible = gui.Visible,
			Text = gui.Text
			-- Add more properties as needed
		}
	end
	
	--checks if player has a datastore if not it will create one
	local success, err = pcall(function()
		print("checking")
		if not dataStore:GetAsync(key) then
			print("Player doesn't have a datastore!")
			dataStore:SetAsync(key, data)
		end
	end)
	
	local success, err = pcall(function()
		dataStore:UpdateAsync(key, function()
			print("updating")
			return data
		end)
	end)

	if not success then
		warn("Failed to save data: " .. tostring(err))
	end
end

local function load(player: Player)
	local key = keyPrefix .. tostring(player.UserId)

	local success, err
	local data
	repeat
		success, err = pcall(function()
			data = dataStore:GetAsync(key)
		end)

	until success or not player:FindFirstChild(player.Name)

	if not data then return end

	if success then
		for guiName, properties in pairs(data) do
			local gui = guisToSave[guiName]
			if gui then
				gui.Visible = properties.Visible
				gui.Text = properties.Text
				-- Apply more properties as needed
			end
		end
	else
		warn("Failed to load data: " .. tostring(err))
	end
end

-- // signals
players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)

game:BindToClose(function()
	for i, plr: Player in ipairs(players:GetPlayers()) do
		print("saving on close of server or something")
		save(plr)
		wait(1)
	end
end)

Okay, i did some testing, some fixing stuff

Actual madlad, god, Deus, Dominus, godheid, deity the almighty. you helped with something i was trying to fix for weeks, u saved me thanks

1 Like

np, what did the issue end up being?

Not saving the gui, yeah thanks alot

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.