I have problems with text and boolean saving

Hi, I have a problem with these script datastores that don’t work: One doesn’t save the text of a textlabel when the player exits and one doesn’t save the “enabled” boolean of a localscript, could someone help me? The API services are enabled

Text Datastore script:

local DataStoreService = game:GetService("DataStoreService")
local TEXT_DATASTORE_KEY = "ShopGCoilText"

local function saveTextToDataStore(player, text)
	local playerDataStore = DataStoreService:GetDataStore(TEXT_DATASTORE_KEY)
	local success, error = pcall(function()
		playerDataStore:SetAsync(tostring(player.UserId), text)
	end)
	if not success then
		warn("Error while saving text to DataStore: " .. tostring(error))
	else
		print("Text saved to DataStore for player " .. player.Name .. ": " .. text)
	end
end

local function loadTextFromDataStore(player)
	local playerDataStore = DataStoreService:GetDataStore(TEXT_DATASTORE_KEY)
	local success, result, error = pcall(function()
		return playerDataStore:GetAsync(tostring(player.UserId))
	end)
	if success then
		print("Text loaded from DataStore for player " .. player.Name .. ": " .. tostring(result))
		return result
	else
		warn("Error while loading text from DataStore: " .. tostring(error))
		return nil
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	local textToSave = game.StarterGui.Shop.Frame.Gcoil.Text
	saveTextToDataStore(player, textToSave)
end)

game.Players.PlayerAdded:Connect(function(player)
	local loadedText = loadTextFromDataStore(player)
	if loadedText then
		game.StarterGui.Shop.Frame.Gcoil.Text = loadedText
	end
end)

Boolean datastore script:

local DataStoreService = game:GetService("DataStoreService")
local BOOL_DATASTORE_KEY = "ShopGCoilEnabled"

game.Players.PlayerRemoving:Connect(function(player)
	local isEnabled = game.StarterGui.Shop.Frame.Gcoil.LocalScript.Enabled
	saveBoolToDataStore(player, isEnabled)
end)

game.Players.PlayerAdded:Connect(function(player)
	local loadedBool = loadBoolFromDataStore(player)
	if loadedBool ~= nil then
		game.StarterGui.Shop.Frame.Gcoil.LocalScript.Enabled = loadedBool
	end
end)

function saveBoolToDataStore(player, boolValue)
	local playerDataStore = DataStoreService:GetDataStore(BOOL_DATASTORE_KEY)
	local success, error = pcall(function()
		playerDataStore:SetAsync(tostring(player.UserId), boolValue)
	end)
	if not success then
		warn("Error " .. tostring(error))
	end
end

function loadBoolFromDataStore(player)
	local playerDataStore = DataStoreService:GetDataStore(BOOL_DATASTORE_KEY)
	local success, result, error = pcall(function()
		return playerDataStore:GetAsync(tostring(player.UserId))
	end)
	if success then
		return result
	else
		warn("Error " .. tostring(error))
		return nil
	end
end

the text isnt being saved because you are trying to access StarterGui instead of PlayerGui. StarterGui is the gui given to the player when they join and it doesnt ever change while playing the game

change all the lines that say StarterGui to PlayerGui

Player.PlayerGui.Shop.Frame.Gcoil.Text = loadedText

im pretty sure you have to create the function before you try to use it so create the functions loadBoolFromDataStore and saveBoolToDataStore before you use them

Done but it doesn’t work.

(character limit)

Done this too but it doesn’t work

ok post the new script so i can keep track

here it is

local DataStoreService = game:GetService("DataStoreService")
local BOOL_DATASTORE_KEY = "ShopGCoilEnabled"

function saveBoolToDataStore(player, boolValue)
    local playerDataStore = DataStoreService:GetDataStore(BOOL_DATASTORE_KEY)
    local success, error = pcall(function()
        playerDataStore:SetAsync(tostring(player.UserId), boolValue)
    end)
    if not success then
        warn("Error " .. tostring(error))
    end
end

function loadBoolFromDataStore(player)
    local playerDataStore = DataStoreService:GetDataStore(BOOL_DATASTORE_KEY)
    local success, result, error = pcall(function()
        return playerDataStore:GetAsync(tostring(player.UserId))
    end)
    if success then
        return result
    else
        warn("Error " .. tostring(error))
        return nil
    end
end

game.Players.PlayerRemoving:Connect(function(player)
    local isEnabled = game.StarterGui.Shop.Frame.Gcoil.LocalScript.Enabled
    saveBoolToDataStore(player, isEnabled)
end)

game.Players.PlayerAdded:Connect(function(player)
    local loadedBool = loadBoolFromDataStore(player)
    if loadedBool ~= nil then
        game.StarterGui.Shop.Frame.Gcoil.LocalScript.Enabled = loadedBool
    end
end)

this is the new script? you havent changed any of the StarterGuis

Should I replace them with playerguis too?

yes, every StarterGui should be a PlayerGui or it will not work

every game.StarterGui should become Player.PlayerGui

StarterGui will clone your guis to the player that joins the server to their PlayerGui so whatever you do on the StarterGui in-game won’t work. You need to access the player’s PlayerGui

I did this and it didn’t work.

(limit)

I found the solution by searching on the forum:

I didn’t think about the fact that there was no need to use datastores.

1 Like

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