Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Do I load and Save BoolValue?

There are 2 scripts, one to save the text when a person leaves

FIRST SCRIPT Loads When a person joins game

BOTH SCRIPTS ARE IN THE WORKSPACE AND ARE REGULAR SCRIPTS

--Things to know about the heirachry - The Thing I want saved is in the StarterGui. -"HasBought" is a boolvalue in the gui thats in the StarterGui - I want to save the value in "HasBought"

local DataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")



game.Players.PlayerAdded:connect(function(player)
	
	local Item = game.StarterGui.Inventory.Inventory.Frame.Item.HasBought
	
	
	
	
	
	local key = "user-"..player.userId
	
	local SavedText = DataStore:GetAsync(key)
	
	
	if SavedText  then
		
		Item.Value = SavedText
		
	else
		local ValuesToSave = {Item.Value}
		DataStore:SetAsync(key , ValuesToSave)
		
		
	end
	
end)

2ND SCRIPT Saves When a Person Leaves

local DataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")



game.Players.PlayerRemoving:connect(function(player)
	
	local key = "player-"..player.userId
	
	
	local ValuesToSave = {game.StarterGui.Inventory.Inventory.Frame.Item.HasBought.Value}
	
	DataStore:SetAsync(key , ValuesToSave)
end)

All Help is Appreciated ^.^

3 answers

In your first script, you save the values into a table (line 23/24).

Then when you load the data (line 15), the data is still in table format and you try to write the table into "Item.Text" (which obviously wants a string).

There are a few ways around it. Firstly, you could simply save the value as the string itself:

local DataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")



game.Players.PlayerAdded:connect(function(player)

    local Item = game.StarterGui.Inventory.Inventory.Frame.Item.HasBought





    local key = "user-"..player.userId

    local SavedText = DataStore:GetAsync(key)


    if SavedText  then

        Item.Text = SavedText

    else
        local ValuesToSave = Item.Text --String rather than table
        DataStore:SetAsync(key , ValuesToSave)


    end

end

Or you could use the index of the table to get the text:

local DataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")



game.Players.PlayerAdded:connect(function(player)

    local Item = game.StarterGui.Inventory.Inventory.Frame.Item.HasBought





    local key = "user-"..player.userId

    local SavedText = DataStore:GetAsync(key)


    if SavedText  then

        Item.Text = SavedText[1] --Using the first index (first value) in the table

    else
        local ValuesToSave = {Item.Text}
        DataStore:SetAsync(key , ValuesToSave)


    end

end)

NOTE: You will need to repeat this for the second script as well

Here's what I know. You're trying to save what the Player's gui shows, but you're using StarterGui instead. So in addition to changing the table value to a string value(as dark had mentioned in his answer) you'll need to change some code around.

local DataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")



game.Players.PlayerAdded:connect(function(player)

   repeat wait() until player.PlayerGui:FindFirstChild("Inventory") -- add a loop here, playergui doesn't load into immediately.
	
    local Item = player.PlayerGui.Inventory.Inventory.Frame.Item.HasBought





    local key = "user-"..player.userId

    local SavedText = DataStore:GetAsync(key)


    if SavedText  then

        Item.Text = SavedText

    else
        local ValuesToSave = item.Text -- good luck with solving the table problem, it's been over 3 months and my thread asking that question hasn't been answer(like all my other questions)
        DataStore:SetAsync(key , ValuesToSave)


    end

end)

do the same thing below

local DataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")



game.Players.PlayerRemoving:connect(function(player)

    repeat wait() until player.PlayerGui:FindFirstChild("Inventory")

    local key = "player-"..player.userId


    local ValuesToSave = {player.PlayerGui.Inventory.Inventory.Frame.Item.HasBought.Text}

    DataStore:SetAsync(key , ValuesToSave)
end)

You might want to insert a value into the player, then add a script into the GUI that changes the value in the GUI to the value in the player's.

This is the script to insert the value in the player, then save it when a player leaves, and to load it when a player joins.

local DataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")

game.Players.PlayerAdded:connect(function(player)
    plrvalue = Instance.new("BoolValue", player)
    plrvalue.Name = "PlayersValue"
    key = "user-"..player.userId

    local SavedText = DataStore:GetAsync(key)


    if SavedText  then

        plrvalue.Value = SavedText[1]

    else
        local ValuesToSave = {plrvalue.Value}
        DataStore:SetAsync(key , ValuesToSave)
    end
end)
-- save the value --
game.Players.PlayerRemoving:connect(function(plr)
	local valuestosave = {plrvalue.Value}
	DataStore:SetAsync(key, valuestosave)
end)

Alright, now here's the script you can put inside the value inside the Inventory.

local plr = game.Players.LocalPlayer -- Must be a localscript

wait(.1) -- just so it doesn't say "PlayersValue is not a member of player"

plr.PlayersValue.Changed:connect(function()
	script.Parent.Value = plr.PlayersValue.Value
end)

NOTE: THE ABOVE SCRIPT MUST BE INSIDE THE VALUE IN THE INVENTORY, AND MUST BE A LOCALSCRIPT!

I hope this helped.. Even though I completely did not understand what you were trying to do. I figured this was the problem.

This discussion is preserved as it was. New answers, comments, and votes are closed.