Save data not saving

Okay guys, this is the 3th time something is wrong with my saving script, so it is kinda stressful. I made a script when I just got started with scripting so it is very messy (you can tell because of the variable names lol), so my apoligy for that (and also for my bad english). I made 3 different data store scripts for 3 different things. And 2/3 work fine, but this one doesn’t restore my data after I join back. the weird thing is that the time period changes a lot. One time it wasn’t working after joining for 4th time, next time it took like 9 joins to fail the restoring. Can someone please help me?

script:

local DataStoreService = game:GetService('DataStoreService')
local myDataStore = DataStoreService:GetDataStore('DataStore')


game.Players.PlayerAdded:Connect(function(player)
	
	
	local girl = Instance.new("Folder",player)
	girl.Name = 'leaderstats'
	
	local upgrades = Instance.new("Folder",player)
	upgrades.Name = 'Upgrades'

	local boy = Instance.new("IntValue",player.leaderstats)
	boy.Name = "Fish"
	
	local tut = Instance.new('BoolValue', player)
	tut.Name = 'TutCompleted'
	
	
	local MP = Instance.new("NumberValue",player)
	MP.Name = "MP"


	local hi = Instance.new("IntValue",player.leaderstats)
	hi.Name = "Coins"
	
	local curboat = Instance.new("StringValue",player)
	curboat.Name = "curboat"
	
	local he = Instance.new("IntValue",player.leaderstats)
	he.Name = "Pearls"
	
	local wood = Instance.new("IntValue",player.leaderstats)
	wood.Name = "Wood"
	
	local petFolder = Instance.new('Folder', player)
	petFolder.Name = 'PetSaves'
	
	local dolphine = Instance.new('BoolValue', petFolder)
	dolphine.Name = 'Dolphine'
	
	local Shark = Instance.new('BoolValue', petFolder)
	Shark.Name = 'Shark'
	
	local Orca = Instance.new('BoolValue', petFolder)
	Orca.Name = 'Orca'
	
	local curD = Instance.new('IntValue', player)
	curD.Name = 'curD'
	
	local CurrentPet = Instance.new('StringValue', petFolder)
	CurrentPet.Name = 'CurrentPet'
	
	local Ku = Instance.new("Folder",player)
	Ku.Name = "ShopSave"
	
	local ho = Instance.new("BoolValue",player.ShopSave)
	ho.Name = "Boat1"
	
	local hq = Instance.new("BoolValue",player.ShopSave)
	hq.Name = "Boat2"
	
	local hz = Instance.new("BoolValue",player.ShopSave)
	hz.Name = "Boat3"
	
	local hQ = Instance.new("BoolValue",player.ShopSave)
	hQ.Name = "Boat4"
	
	local ha = Instance.new("BoolValue",player.ShopSave)
	ha.Name = "Boat5"
	
	local hw = Instance.new("BoolValue",player.ShopSave)
	hw.Name = "Boat6"
	
	local AA = Instance.new("BoolValue",player.ShopSave)
	AA.Name = "Boat7"
	
	local BB = Instance.new("BoolValue",player.ShopSave)
	BB.Name = "Boat8"
	
	local CC = Instance.new("BoolValue",player.ShopSave)
	CC.Name = "Boat9"
	
	local WW = Instance.new("BoolValue",player.ShopSave)
	WW.Name = "Island2"
	
	local Helicopters = Instance.new('IntValue', player)
	Helicopters.Name = 'Helicopters'
	
	local MAX = Instance.new('IntValue', Helicopters)
	MAX.Name = 'MAX'
	
	local WAA = Instance.new("BoolValue",player.ShopSave)
	WAA.Name = "Quest1"
	
	wait()
	
	local playerUserId = "Player_"..player.UserId
	
	local data
	local succes, errormessage = pcall(function()
		
		data = myDataStore:GetAsync(playerUserId)
		
	end)
	
	
	
	if succes then
		
		wood.Value = data.Wood
		boy.Value = data.Fish
		hi.Value = data.Coins
		he.Value = data.Pearls
		ho.Value = data.Boat1
		hq.Value = data.Boat2
		hz.Value = data.Boat3
		hQ.Value = data.Boat4
		ha.Value = data.Boat5
		hw.Value = data.Boat6
		AA.Value = data.Boat7
		BB.Value = data.Boat8
		CC.Value = data.Boat9
		WW.Value = data.Island2
		Helicopters.Value = data.Helicopters
		tut.Value = data.TutCompleted
		MP.Value = data.MP
		dolphine.Value = data.Dolphine
		Shark.Value = data.Shark
		Orca.Value = data.Orca
		curD.Value = data.curD
		MAX.Value = data.MAX
		curboat.Value = data.curboat
		
		if MAX.Value == 0 then
			
			MAX.Value = 1
			
		end
		
		
		if curboat.Value == '' then
			
			curboat.Value = 'Boat'
			
		end
		
		if data.CurrentPet == nil then
			
		else
			
			CurrentPet.Value = data.CurrentPet
			
		end
		
		
	end
	if MP.Value == 0 then
		
		MP.Value = 1
		
	end
	if MP.Value == 2 then
		
		game.Workspace.Sell.Salebord:Destroy()
		
	end
	for i,v in ipairs(player:WaitForChild('PlayerGui'):WaitForChild('SellUpgradeGui').Frame.Upgrades:GetChildren()) do
		
		if v.Value == MP.Value then
			
			for p,a in pairs(game.Workspace:FindFirstChild('SellUpgrades'):FindFirstChild(v.Name):GetChildren()) do
				
				a.Transparency = 0
				a.CanCollide = true
				
			end
			
			
		end
		
	end
	local function PetEquip()

		game.Workspace.Pet:ClearAllChildren()

		local newPet = game:GetService('ReplicatedStorage').Pets:FindFirstChild(CurrentPet.Value):Clone()
		newPet.Parent = game.Workspace.Pet
		newPet:FindFirstChild('PLNAME').Value = player.Name
		newPet:FindFirstChild('Script').Enabled = true

	end
	if not (CurrentPet.Value == '') then
		
		PetEquip()
		
	end
	for i,v in pairs(petFolder:GetChildren()) do
		
		if v:IsA('BoolValue') then
			
			if v.Value == true then
				
				player:WaitForChild('PlayerGui'):WaitForChild('PetShopGui'):FindFirstChild(v.Name).Buttons.Buy:Destroy()
				if CurrentPet.Value == v.Name then
					
					player:WaitForChild('PlayerGui'):WaitForChild('PetShopGui'):FindFirstChild(v.Name).Buttons.Equip.Visible = false
					player:WaitForChild('PlayerGui'):WaitForChild('PetShopGui'):FindFirstChild(v.Name).Buttons.Equipped.Visible = true
					
				else
					
					player:WaitForChild('PlayerGui'):WaitForChild('PetShopGui'):FindFirstChild(v.Name).Buttons.Equip.Visible = true
					player:WaitForChild('PlayerGui'):WaitForChild('PetShopGui'):FindFirstChild(v.Name).Buttons.Equipped.Visible = false
					
				end
				
			else
				
				player:WaitForChild('PlayerGui'):WaitForChild('PetShopGui'):FindFirstChild(v.Name).Buttons.Equip.Visible = false
				player:WaitForChild('PlayerGui'):WaitForChild('PetShopGui'):FindFirstChild(v.Name).Buttons.Buy.Visible = true
				
			end
			
			
		end
		 
	end
	
	
 end)
game.Players.PlayerRemoving:Connect(function(player)
	
	local playerUserId = "Player_"..player.UserId
	
	local data = {
		curD = player.curD.Value;
		Boat2 = player.ShopSave.Boat2.Value;
		Boat3 = player.ShopSave.Boat3.Value;
		Boat4 = player.ShopSave.Boat4.Value;
		Boat5 = player.ShopSave.Boat5.Value;
		Boat1 = player.ShopSave.Boat1.Value;
		Fish = player.leaderstats.Fish.Value;
		Coins = player.leaderstats.Coins.Value;
		Pearls = player.leaderstats.Pearls.Value;
		Boat6 = player.ShopSave.Boat6.Value;
		Boat7 = player.ShopSave.Boat7.Value;
		MAX = player.Helicopters.MAX.Value;
		Boat8 = player.ShopSave.Boat8.Value;
		Boat9 = player.ShopSave.Boat9.Value;
		Island2 = player.ShopSave.Island2.Value;
		Wood = player.leaderstats.Wood.Value;
		MP = player.MP.Value;
		Helicopters = player.Helicopters.Value;
		Dolphine = player.PetSaves.Dolphine.Value;
		Shark = player.PetSaves.Shark.Value;
		Orca = player.PetSaves.Orca.Value;
		CurrentPet = player.PetSaves.CurrentPet.Value;
		curboat = player.curboat.Value;
		TutCompleted = player.TutCompleted.Value;
		
		
	}
	
	local succes, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
	
	if succes then
		print('Saved Succesfully!')
		
	else
		print('saving error!')
		warn(errormessage)
		
	end
end)

game:BindToClose(function()
	wait(3)
end)
1 Like

game:BindToClose() needs to be the exact same function as playerRemoving to every player left in the server, because sometimes the server shuts down without firing playerRemoving due to issues. This also explains why it sometimes fails, not frequently.

game:BindToClose(function()
for i,player in pairs(game.Players:GetPlayers()) do
-- save data
end
wait(2)
end)

OOh thanks! So wich part of the script do I need to put in the BindToClose function then? I’m still learning so I’m not sure.

you have a BindToClose() function at the end already. Just overwrite that one because it only has a wait(), which is useless if you’re not doing anything with it