Trying to save a lot of bool values at a time. But it's crashing

I’m trying to save a lot of bool values at a time. But it’s crashing

I got this script and modified it from the dev forum
Please tell me what I’m doing wrong, Thanks!

local DataStoreService = game:GetService("DataStoreService");
local DataStore = DataStoreService:GetDataStore("RopeService");

game.Players.PlayerAdded:Connect(function(player)
	for _, ropeBadge in pairs(workspace.Badges:GetChildren()) do
		if ropeBadge:IsA("StringValue") then
			local boolval = Instance.new("BoolValue", player)
			boolval.Name = ropeBadge.Value
			
			boolval.Value = DataStore:GetAsync(player.UserId) or false

			if boolval.Value == true then
				print(ropeBadge.Value .. " is owned by " .. player.Name)
			end
			
			
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for _, ropeBadge in pairs(workspace.Badges:GetChildren()) do
		if ropeBadge:IsA("StringValue") then
			
			coroutine.resume(coroutine.create(
				function()
					DataStore:SetAsync(player.UserId, player:FindFirstChild(ropeBadge.Value).Value)
				end
				
				)
			)
		end
	end
end)
1 Like

Btw, I’m trying to make a saving system for the players Gui Inventory (that already works)

You are requesting “GetAsync” way too many times. Since it is all the same value, just define a variable that is equal to “DataStore:GetAsync(player.UserId) or false” on line 5 and just set boolval.Value to that variable.

i tried to do that and it still shows the “Not running script because past shutdown deadline (x37)” error
am i doing it right?

local DataStoreService = game:GetService("DataStoreService");
local DataStore = DataStoreService:GetDataStore("RopeService");

game.Players.PlayerAdded:Connect(function(player)
	local a = DataStore:GetAsync(player.UserId) or false
	
	for _, ropeBadge in pairs(workspace.Badges:GetChildren()) do
		if ropeBadge:IsA("StringValue") then
			local boolval = Instance.new("BoolValue", player)
			boolval.Name = ropeBadge.Value
			
			boolval.Value = a
			
			if boolval.Value == true then
				print(ropeBadge.Value .. " is owned by " .. player.Name)
			end
			
			
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for _, ropeBadge in pairs(workspace.Badges:GetChildren()) do
		if ropeBadge:IsA("StringValue") then
			
			coroutine.resume(coroutine.create(
				function()
					DataStore:SetAsync(player.UserId, player:FindFirstChild(ropeBadge.Value).Value)
				end
				
				)
			)
		end
	end
end)

Wait I think your datastore is a bit off. You are only saving 1 boolean. Use the code from this post to save your data properly:

i tried to modify the script but it still didnt work

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data
	

	for _, badge in pairs(workspace.Badges:GetChildren()) do
		if badge:IsA("StringValue") then
			local tableToSave = {
				player[badge.Value].Value
			}

			local success, err = pcall(function()
				dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
			end)

			if success then -- If the data has been saved
				print("Data has been saved!")
			else -- Else if the save failed
				print("Data hasn't been saved!")
				warn(err)		
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
	local d = 0
	
	
	for _, badge in pairs(workspace.Badges:GetChildren()) do
		if badge:IsA("StringValue") then
			local hasRope = Instance.new("BoolValue", player)
			hasRope.Name = badge.Value
			
			local data -- We will define the data here so we can use it later, this data is the table we saved
			local success, err = pcall(function()

				data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore

			end)

			if success and data then -- If there were no errors and player loaded the data

				hasRope.Value = data[d + 1] -- Set the money to the first value of the table (data)

			else -- The player didn't load in the data, and probably is a new player
				print("The player has no data!") -- The default will be set to 0
			end
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

You are still only saving 1 value. Read your savedata function again. Also your load data is incorrect too.

no im making a new bool value for every badge value

Every time you save the data you are overwritting the previously saved value.

The way I do it is I make a dictionary(index is the value name and the value is the actual value) and save the whole dictionary with set async, then I use getasync to get the dictionary and transfer the values from the dictionary.

Here’s the difference between a dictionary and a table

Dictionary:

dictionary ={[“apples”] = 1, [val] = 3]

Table:

tablesample = {“Lol”, workspace.Part}

i find it better by doing

for _, object in pairs(workspace.Objects:GetChildren())

end

how should i do that

Create a table by doing local table = {}
Then for each value use table.insert to add it to your datatable. Then you can save the completed datatable using setasync

If you want to save the index as well you can do

insert_table_name_here[i] = v

or the way you created the for loop variables

insert_table_name_here[_] = object

i find it better by doing

for _, object in pairs(workspace.Objects:GetChildren())

end

how should i do that