Problem with saving players data

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    For the players data to be saved upon exit of the game/studio

  2. **What is the issue so far **
    Well before all the data was saving, and now it doesn’t want to save

  3. What solutions have you tried so far?
    I have tried looking on the hub for help and ways to fix it but I still haven’t be able to fix the issue

It was saving recently when the player left the game and rejoined, they would still have their stats, but now when they join its as if they have never played before

2 Likes

Can you show us the script you use to save their data?

You just have to get the PlayerRemoving event

Edit: oh wait didn’t see it was working but now it isn’t could I please see the code?

1 Like

yeah sure thing

local DS = game:GetService("DataStoreService"):GetDataStore("MyDataSaver")

game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character.Humanoid
	wait()
	local plr_key = "id_"..player.UserId
	
	local saves = {
		----------------------
		   ---Class Info---
		----------------------
		player.Class,
		
		----------------------
		--Stats Saving Info--
		----------------------
		player.Coins,
		player.TotalCoins,
		player.Sapphires,
		player.TotalSapphires,
		player.Strength,
		player.Sword,
		player.Agility,
		player.JumpPower,
		player.Psp,
		player.Endurance,
		player["Total Power"],
		
		----------------------
		---Multipliers Info---
		----------------------
		player.Multi,
		player.eMulti,
		player.pMulti,
		player.aMulti,
		player.sMulti,	
		player.jMulti,
		----------------------
		   ---Quest Info---
		----------------------
		player.questNum,
		player.hasClickedQuest,
		player.QuestInfo.Stat,
		player.QuestInfo["Value"],
		-------SwordQuest--------
		player.SwordquestNum,
		player.hasClickedQuest,
		player.SwordQuestInfo.SwordStat,
		player.SwordQuestInfo["SwordValue"],
		----------------------
		---Safe Zone Info---
		----------------------
		player.Safe,
		
		----------------------
		--Powers Saving Info--
		----------------------
		player.Powers.hasAura,
		player.Powers.hasFly,
		player.Powers.hasEnergyPunch,
		player.Powers.hasInvisiblity,
		player.Powers.hasDefBall
		
	}
	
	-- SAVING DATA
	local GetSaved = DS:GetAsync(plr_key)
	if GetSaved then
		print("Has Played Before!")
		for i, v in pairs(saves) do
			v.Value = GetSaved[i]
		end
	else
		print("New Player!")
		wait(1)
		local plrGui = player:WaitForChild("PlayerGui")
		local frame = plrGui:WaitForChild("TutorialGUI"):WaitForChild("Frame")
		local text = frame:WaitForChild("TutorialText")
		frame.Visible = true
		text.Text ="Welcome "..player.DisplayName..", to The Server!"
		
		local NFS = {}
		for i, v in pairs (saves) do
			table.insert(NFS, i, v.Value)
		end
		DS:SetAsync(plr_key, NFS)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character.Humanoid
	local saves = {
		---Class Info---
		----------------------
		player.Class.Value,

		----------------------
		--Stats Saving Info--
		----------------------
		player.Coins.Value,
		player.TotalCoins.Value,
		player.Sapphires.Value,
		player.TotalSapphires.Value,
		player.Strength.Value,
		player.Sword.Value,
		player.Agility.Value,
		player.JumpPower.Value,
		player.Psp.Value,
		player.Endurance.Value,
		player["Total Power"].Value,
		
		----------------------
		---Multipliers Info---
		----------------------
		player.Multi.Value,
		player.eMulti.Value,
		player.pMulti.Value,
		player.aMulti.Value,
		player.sMulti.Value,
		player.jMulti.Value,
		
		----------------------
		---Quest Info---
		----------------------
		player.questNum.Value,
		player.hasClickedQuest.Value,
		player.QuestInfo.Stat.Value,
		player.QuestInfo["Value"].Value,
		-------SwordQuest--------
		player.SwordquestNum.Value,
		player.hasClickedQuest.Value,
		player.SwordQuestInfo.SwordStat.Value,
		player.SwordQuestInfo["SwordValue"].Value,
		----------------------
		---Safe Zone Info---
		----------------------
		player.Safe.Value,
		----------------------
		--Powers Saving Info--
		----------------------
		player.Powers.hasAura.Value,
		player.Powers.hasFly.Value,
		player.Powers.hasEnergyPunch.Value,
		player.Powers.hasInvisiblity.Value,
		player.Powers.hasDefBall.Value
		
	}
	
	DS:SetAsync("id_"..player.UserId, saves)
end)

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

I think it’s impossible to have a datastore script that saves 100% of the time. I’m not sure how those popular games do it, but most of them have and autosave. The most simple way is to save data when the player leaves. I’m not sure how to save multiple items however like you’re doing.

I was able to fix the problem by removing a part of the script

Okay will could you post the script again in case anybody else has the same problem, and also mark the topic as solved.

Yeah just noticed that it didn’t save :frowning:

I think what happens is that the server gets destroyed before the script is done running, and in that case I have no idea how to prevent that from happening because I believe you have no control over that.

Yeah like I left it on for about 5 mins, doing the things in game, left and rejoined was none saved

Their is a part though that says if the player is new or has played before, and each time says I have

Everytime I have progressed, I’m always set back by this issue when it happens, just wished I could have figured it out

I would make it save every 90 seconds and also save when the person leaves

If you’re doing this in Studio, then I think I know why there’s a problem. The ROBLOX servers’ code is to close a bit after the last player leaves. But in Studio, for memory, it closes immediately. So I see you’ve used game:BindToClose(), but change the wait(3) to a for loop that goes through all the players and saves their data.

3 Likes

Thanks for replying I will have a look into looping it as it closes to save the players data