Database won't save... [Datastore2 Usage, Please Help]

Fixed Code
--LOADING DATA--
local serverData,hostData,GamemodeData,difficultyData,dataCodeData
local success,errorr = pcall(function()
	serverData = dataServer:Get()
	hostData = dataHost:Get()
	GamemodeData = dataGamemode:Get()
	difficultyData = dataDifficulty:Get()
	dataCodeData = dataCode:Get()
end)


if success then
	if serverData and hostData and GamemodeData and difficultyData and dataCodeData then
		Server.Value = serverData
		Host.Value = hostData
		Gamemode.Value = GamemodeData
		Difficulty.Value = difficultyData
		Code.Value = dataCodeData
	else
		Server.Value = "None"
		Host.Value = "None"
		Gamemode.Value = 0
		Difficulty.Value = 0
		Code.Value = 0
	end
elseif errorr then
	error(errorr)
end

Hmm so its a problem with saving… let’s see :thinking:

Saving part code, added pcall function to check errors.
--SAVING DATA--
local yes,no = pcall(function()
	Server.Changed:Connect(function()
		dataServer:Set(Server.Value)
	end)
	Host.Changed:Connect(function()
		dataHost:Set(Host.Value)
	end)
	Gamemode.Changed:Connect(function()
		dataGamemode:Set(Gamemode.Value)
	end)
	Difficulty.Changed:Connect(function()
		dataDifficulty:Set(Difficulty.Value)
	end)
	Code.Changed:Connect(function()
		dataCode:Set(Code.Value)
	end)
end)

if yes then
	print("Saved data for "..plr.Name)
elseif no then
	error(no)
end

I am back and I tried it, sadly it doesn’t work.

I am not good with DataStore2, and since you are not storing any Tables value.
Why not use roblox’s normal datastore with :SetAsync?

People says setAsync makes you lose data but only for tables not for single values.
I also use roblox’s datastore but since i have tables, I use :UpdateAsync and haven’t got any data lose reports.

It’s not my first time making this topic, if you check my profile you will see how much I searched for a solution regarding this problem. I will not use Roblox’s datastore again since it has no solution found.

Maybe you were using datastore in wrong way, i’ve checked one of your topic and found this


well the value you set isn’t only Value its the properties of CashValue itself.
So you would have used :SetAsync(plr.UserId,CashValue.Value)
instead of using :SetAsync(plr.UserId,v)

But still, I haven’t tried DataStore2 because I have no problems using roblox’s normal datastore.

Are your events firing? I also think you should structure your code a little differently. You can even look back at the DataStore2 post which explains a lot on how to use it. There are even hundreds of comments to look through which might answer some problems (not to mention a tutorial AlvinBlox created to go more into depth).

2 Likes

I wouldn’t encourage somebody to go back to the regular Datastore just because they are having an issue solving a problem with Datastore2. With time and patience Doom can figure out a solution for his issue.

Also data loss has nothing to really do with SetAsync, that is just how you handle data yourself. I’m pretty sure Datastore2 was created to make it easier for data loss to be prevented.

2 Likes

Well, as I said before I’ve never used DataStore2 because I have no problem with normal one.
And :SetAsync plays a huge role on losing data.
SetAsync is made for saving single values not Tables, while :UpdateAsync helps alot.

1 Like

I discovered the DataStore2 today and I searched for a tutorial (which used the code I am using right now). And for other people (in the comments) worked, including for the guy that made the tutorial obviously. I have also seen Nosity’s tutorial but it seemed too complicated for me (since I am a builder and I want to learn scripting). The tutorial I used is here tutorial

1 Like

I assume that’s me! XD If I do know what tutorial you are talking about, that was based on saving tables to DataStore2 which can get a little complicated and is unnecessary especially if you don’t need to save tables.

I think the main issue though is that these events aren’t being triggered, try putting an if statement in between them and see if they are triggered. If everything seems to be saving correctly then it could just be how you are loading your data.

1 Like

Again this goes to more how people handle their datastores, but I can see the point you are trying to make, thanks!

1 Like

Or maybe some remoteevents in replicated storage would work??

That is what I use to change my DataStore2 values. Just one remote event with parameters that determine what to change and where to change it. Although that kind of ignored my statement of checking to see if the events are being triggered in your code. If you would like to go for the remote event rounte then go ahead! Although I would check to see if your events are being triggered first to make sure that you aren’t actually saving values.

1 Like

I’ll first try to see if the events are being triggered. Where do I put the if statement? Tbh idk

oh no, when we are debugging code we use print() statements and check the output. I meant you should print something inside of each of your events to see if it triggers correctly. If it doesn’t print anything then we found the issue.

1 Like

Screenshot 2021-12-30 124747

The only thing that prints is the one from the end. (aka Saved data for player.Name)
which is kinda weird.

Yeah your events .Changed are not triggering, this means your value isnt changing.
You can try PlayerRemoving instead of using .Changed

I tried that too in my other scripts. It didn’t work.

Well I am not sure why a pcall(function() is necessary for this, but I assume the Changed event isn’t being triggered through any of your values which explains why your data isn’t saving. I would personally remake my save and create a remote event which can be used to change the values in your Datastore. From the client you can send the information that you want to update and then in your Datastore script you change it from there.

1 Like

I really don’t know how to remake since that’s the best I could do. I am bad with remote events even though I have some setup, I knew it was gonna come to remotes…