Saving the bool instance and it's value?

Hello guys I’m new to script and I’m facing an error. I made a script that saves the bool values from the player’s folder (which is called “ownership”). Now my problem is that sometimes when I rejoin it saves the previous bool values that the new ones. For instance, I have 3 bool values, first called “first”, second called “second” and third called “third”.

1st play test: I set the first = true, second = false, third = false (when the player leaves; the script saves these values and when he rejoins he will have these values)

2st play test: I set the first = false, second = true, third = false

3st play test: I joined the game and have the values that correspond to the 1st play test

if you have any idea please let me know. The error occurs randomly

here is the script:

local titlefolder = game:GetService("ReplicatedStorage"):FindFirstChild("Titles") -- this is the folder that contains all the values

local datastore = game:GetService("DataStoreService")

local savedata = datastore:GetDataStore("SaveTitles")


game.Players.PlayerAdded:Connect(function(plr)
	wait(.5)
	
	local data
	
	local ownership = plr:WaitForChild("ownership") -- a folder inside of the player

	local success,errormessage = pcall(function()
		data = savedata:GetAsync(plr.UserId)
	end)
	
	if data ~= nil then
		if success then
			for i,v in pairs (data) do
				if ownership:FindFirstChild(i) == nil and titlefolder:FindFirstChild(i) then
					local clone = titlefolder:FindFirstChild(i):Clone()
					clone.Parent = ownership
					clone.Value = v
				end
			end
		end
	end
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetPlayers()) do
		plr:Kick() -- Apparently so that it fires the PlayerRemoving function?
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	wait(1)
	local itemsowned = {}
	local ownership = plr:WaitForChild("ownership")
	
	for i,v in pairs (ownership:GetChildren()) do
		itemsowned[v.Name] = v.Value
	end
	
	--for i,v in pairs (itemsowned) do (This is for testing purposes checking if it is saving the correct data)
		--print(i.." = "..tostring(v))
	--end
	local success,errormessage = pcall(function()
		savedata:SetAsync(plr.UserId, itemsowned)
	end)
	
	if success then
		print("Titles successfully saved")
	else
		warn(errormessage)
	end
end)

Does it Warn you on the second time?(I know it should not warn you if it worked the 3rd time you play) and did it say “Titles successfully saved” in the output within of all testing?

Not warnings nothing, and it is saying Titles successfully saved. The problem with that is that it happens randomly I don’t understand the issue.

Maybe don’t add the “wait(1)” in player added or removed, it is useless

Oh ok, I will try that. I thought that the wait function would guarantee the save of the values

try printing data, could it be nil or not

I have already tried that. Data is never nil the problem is that sometimes the wrong values are saved. For instance; (When Leaving Game) Let’s say I have three values inside of the ownership folder (Yes = true, Maybe = false, No = false). (When Joining Game) It will save it just fine. Let’s change our values to (Yes = false, Maybe = true, No = false) and then we leave the game. After joining back our values corresponds to the first (Yes = true, Maybe = false, No = false) instead of (Yes = false, Maybe = true, No = false). This problem happens randomly.

I actually found the reason that doesn’t work. In Roblox studio sometimes datastore doesn’t work due to instant closing of the game which lead to data loss. I tried opening an actual server each time and it seems that it is working

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.