Problem with my script for Datastore

I’ve been seeing so many Scripting Support requests about a data store script not working.

Could you try to use Google to get an idea of how it works, to then check what you have missed out on from that, or anything?

I do understand you are new to scripting, but try a YouTube video first. That can help.

I learned this script somehow from YouTube only and modified my own but it didn’t work out that’s why I asked here!

When I tested the script, it always prints “Assigning Default Data” which means that data doesn’t exist and it is overwrited. My fix doesn’t print that anymore and works perfectly (saves everything)

every second or each time you join?

Every second and subsequent joins, for the original script it always prints no matter how many times I joined

yeah me too when i join the game it prints out that but maybe when the player joins it might saving the data?

It’s something to do with pcalls. It sets success and errorMessage to a value, if success is true then errorMessage would be set to nil, if success is false then errorMessage would be a string telling what went wrong.
It could be why playerdata keeps being overwritten and printing it doesn’t exist, thus setting up new data for the player

but what if the data changes then it would not store it because it the succes is true?

Not entirely sure, maybe your mismatching different values on accident? Are you getting any error messages? Maybe you should use table.find(sessiondata[plr.UserId), “Button2”)…

Sorry bad service, seems like that’s not the problem.

It only sets data to nil once, not every time. You have Changed connected already. So yea try the script above

idk about it but i think it is matching because when the button value changes it changes inside player leaderstats and the datastore detects the value and changes it. because if it can detect and store cash value then why it can’t store other values?

yeah but i learned (copied) from the youtube video, they can’t be wrong right?

It depends on the amount of views, the video’s age, and the comments, you can’t trust youtubers completely. You can try my script and tell me if it works.

i mean the video is quite popular! And well explained!

Have you tried using the BindToClose function? It’s an event that pauses the game and lets some run code, including task.wait()s:

game:BindToClose(function()
    task.wait(5)
end)

This waits 5 seconds before entirely closing the game, to let the data save. Put this code before anything else, so it works properly.

Okay this is getting a bit off topic, if you want to discuss about the video then we can do it in DMs.
By the way have you tried my code yet? Mark the code as a solution if it works

It’s a problem with loading data, not saving data. Still a good way to prevent data loss though

Uhh well nope it didn’t work out:(

Yeah but why we need to pause the script?