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)