Engine bug? saving not working as intended

So I made a script which creates bool values based on the amount of weapons on a folder using for loops, and saves it to a datastore. However when one of the bool values gets updated to “true” value, It loads it on every other bool values that shouldnt have a “true” value. It only gets set to “true” when they have already bought the weapon.
Here’s the script:

local SVersion = DS:GetDataStore("SaveTest1")

game.Players.PlayerAdded:Connect(function(player)
	local SStorage = game:GetService("ServerStorage")
	local WeapFold = SStorage:WaitForChild("Swords")
	
	local WeaponSaves = Instance.new("Folder", player)
	WeaponSaves.Name = 'WeaponSaves'

	for i,Weaps in pairs(WeapFold:GetChildren()) do
		local BoolValue = Instance.new("BoolValue", WeaponSaves)
		BoolValue.Name = Weaps.Name	
		BoolValue.Value = SVersion:GetAsync(player.UserId) or false --The values loads the "True" value from one bool to every other values which ruins the data saving.
	
	end	
end)

--Once player leaves, save their owned weapon values to true and move it to a table.

game.Players.PlayerRemoving:Connect(function(plr)

	
	local Data1 = {}
	local SavesFolder = plr:FindFirstChild("WeaponSaves")
	for i,Values in pairs(SavesFolder:GetChildren()) do
		table.insert(Data1, Values.Value)
		
		local success,response = pcall(function()
			SVersion:SetAsync(plr.UserId, Data1)
		end)

		if success then		
		else
			warn(response)
		end
	end
end)

Here is the photo where the player bought weapon 4, It turned the value to “True”
The datasave should only save the “True” Value for weapon 4 but for some reason when the player loads again, other values also get their values to “True” even if they haven’t bought that weapon yet.
image

Picture of all the values getting turned to “True” even if they haven’t bought it yet, The result of Weapon 4’s “True” Value getting loaded to every other values.
image

I remember encountering something like this earlier, the issue you’re dealing with is that there’s no difference for each BoolValue you’re trying to obtain via using GetAsync(), (Also I highly encourage you to encase that in a pcall() function as well)

The way I fixed it, was adding another table to my Weapon Data by refencing both the Weapon’s Name, and Value so that way you can differentiate between which weapon is which

game.Players.PlayerRemoving:Connect(function(Pr)
    local Data1 = {}
    local SavesFolder = Plr:WaitForChild("WeaponSaved")

    for _, Bool in pairs(SavesFolder:GetChildren()) do
        table.insert(Data1, {Bool.Name, Bool.Value}
    end

    local success, whoops = pcall(function()
        SVersion:SetAsync(Plr.UserId, Data1)
    end)

    if success then
        print("Yes")
    else
        warn(whoops)
    end
end)

And when you call your Data using GetAsync() when a Player joins, loop through the Data inside that data as well (Table-Ception basiaclly)


game.Players.PlayerAdded:Connect(function(Plr)
	local SStorage = game:GetService("ServerStorage")
	local WeapFold = SStorage:WaitForChild("Swords")

	local WeaponSaves = Instance.new("Folder")
	WeaponSaves.Name = 'WeaponSaves'
	WeaponSaves.Parent = Plr
	
	for i,Weaps in pairs(WeapFold:GetChildren()) do
		local BoolValue = Instance.new("BoolValue")
		BoolValue.Name = Weaps.Name	
		BoolValue.Parent = WeaponSaves
	end	
	
	local Data
	local success, whoops = pcall(function()
		Data = SVersion:GetAsync(Plr.UserId)
	end)
	
	if success then
		if Data ~= nil then
			for _, WeaponTable in pairs(Data) do

				local StatCheck = WeaponSaves:FindFirstChild(WeaponTable[1])

				if StatCheck then					
					StatCheck.Value = WeaponTable[2]
				end
			end
		end
	end
end)

That way it can check to make sure that every individual weapon is different cause they have unique Names, and Values

Hey thanks for the help, for some reason when I tried doing this method, this line for _, WeaponTable in pairs(Data) do Returns an error:

Is it because that I’m saving boolean values to the Data variable and that caused this line to send an error output?

I think this is because you have the old data saved from the last datastore, cause we’re changing your Data from a Bool, to a table

Try running this line first, then playing the script again to refresh it?

local DSS = game:GetService("DataStoreService")
local SVersion = DSS:GetDataStore("SaveTest1")
SVersion:RemoveAsync(InsertYourIdHere)

Works perfectly! thanks for your method really helped me

Np! Btw, make sure to mark my post as the solution so that other people know that it’s been resolved!

1 Like