DataStore Could Not Save Gui settings

So i have been making my fully based gui font viewer game in roblox, i added a themes option and soon will add a word editor, now lets focus on the themes, the i have 2 themes, default and green, and the path to each of those is game.startergui.screengui.themeframe.default game.startergui.screengui.themeframe.gren

when i change between those, it works but when i rejoin the the default theme always shows really appreciate your help

i tried chatgpt but chatgpt's code never works.

Upvotes: 0

Views: 35

Answers (1)

vijaysharma
vijaysharma

Reputation: 61

It is important to note that data stores can store any kind of data you want (if you encode it). In your case, it seems that you just want to store a single string, which is the theme that the user has selected. Assuming this is a client-sided application, you will want a remote event to get the theme and one to set the theme. This is because data-stores can only be accessed from the server.

The Roblox documentation has articles on Remote Events and Callbacks and on Data Stores.

Here is some code that I wrote up that handles saving and retrieving data from data-stores through a remote event called GetTheme and a remote function called UpdateTheme, both of which are inside ReplicatedStorage. The script itself should be in ServerScriptService as a server-sided script.

local THEME_DATASTORE_NAME = "Themes"

local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local themeDataStore = DataStoreService:GetDataStore(THEME_DATASTORE_NAME)

local getThemeRemoteEvent = ReplicatedStorage.GetTheme
local updateThemeRemoteFunction = ReplicatedStorage.UpdateTheme

-- We want to return true if this was successful, and false if it was not,
-- so that the client can display an appropriate error to the user.
updateThemeRemoteFunction.OnServerInvoke = function(player, theme)
    if theme == "default" or theme == "green" then
        local wasSuccess, result = pcall(function()
            themeDataStore:SetAsync(player.UserId, theme)
        end)

        if not wasSuccess then
            print("Unable to save data to data-store: " .. result)
        end

        return wasSuccess
    end

    return false
end

getThemeRemoteEvent.OnServerEvent:Connect(function(player)
    -- Retrieve the theme that the user saved
    return themeDataStore:GetAsync(player.UserId) or "default"
end)

Edit: It is important to note here that I did not implement any rate limiting, which I would highly recommend. All you would need to do in this case is have a have a table of players and their last theme update / retrieval and make sure that they can only perform that operation after, say, 5-10 seconds.

Upvotes: 0

Related Questions