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
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
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)
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