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

If one bool value is true then all others are made true too when saving data, why?

I have a folder that stores a couple of bool values, the folder is stored in ReplicatedStorage and I have a saving script that saves the values of the bool values, the problem is when I test the game and data works in studio btw, I have a shop and when I buy something the bool value of that specific item is set to true, then I leave the game, I rejoin and then find that all bool values are set to true and everything item is like "Equip" why is this happening when saving?

theres no error in the output, just prints that data got saved..

Serverscript: (saving script)

-- Services

local DataStoreService = game:GetService("DataStoreService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TagData = DataStoreService:GetDataStore("Test1")

-- Folders

local nametagPurchases = ReplicatedStorage:WaitForChild("NametagPurchases")

local firstPage = nametagPurchases.FirstPage

local function getTags() -- A function that gathers all the Bool Values and are store in a table.
	
	local Tags = {} -- The table the boolvalues are stored in.
	
	for _,tag in pairs(firstPage:GetChildren()) do
		
		if tag:IsA("BoolValue") then
			
			Tags[tag.Name] = tag.Value
			
		end
		
	end
	
	return Tags
	
end

game.Players.PlayerAdded:Connect(function(player)
	
	local TagValues = getTags() -- Variable that gets the table from the function
	
	local data
	
	local success, err = pcall(function()
		data = TagData:GetAsync(player.UserId) -- get data
	end)
	
	if success then
		
		 for _,boolvalue in pairs(firstPage:GetChildren()) do -- Goes through all boolvalues in the folder and saves them one by one.
			if boolvalue:IsA('BoolValue') then
				
				boolvalue.Value = data
				
			end
		end
		
	else
		print("There was an error getting the tag values when player was added")
		warn(err)
		
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local TagValues2 = getTags() -- gets table
	
	local success,err = pcall(function()
		
		TagData:SetAsync(player.UserId, TagValues2) -- saves the data
		
	end)
	
	if success then
		
		print("All tag values are saved!")
		
	else
		print("There was an error saving the tag values when player left.")
		warn(err)
		
	end	
	
end)

pls help pls pls

1 answer

The only thing I can see right now is that when you saving the data, you are only getting 1 piece of data and applying that same one to all the bool values, which is why they all have the same value. Due to this, you would need to create new data stores and name it uniquely to the exact value you want to apply it to, so that you don’t mix them up. Inside your for loop where you are doing “for _,boolvalue in pairs(firstPage:GetChildren()) do”, instead of taking each value and applying the same data, you can create a new data store for each boolvalue that’s in the table.

I believe this can be done somewhat like this:

for _,boolvalue in pairs(firstPage:GetChildren()) do
if boolvalue:IsA(‘BoolValue’) then
local BoolData = DataStoreService:GetDataStore(boolvalue.Name, boolvalue.Value)
boolvalue.Value = BoolData
end
end

Let me know if this doesn’t work or doesn’t match what you want. I’m a bit confused on this as well. ;)

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