Problem with my script for Datastore

Hey everyone! I am a beginner scripter, and I just made a datastore functions to save the player data. this is my first time I did datastore and I stumbled upon a problem. Can you fix this code?

here only cash value saves and others do not save.

local gamepassID = 259204857
local gamespass2xID = 259206466
local gamespass5xID = 259207041
local gamespass3xID = 259215442
local datastoreservice = game:GetService("DataStoreService")

local database = datastoreservice:GetDataStore("data")
local sessiondata = {}

 local function Playeradded(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name =  "leaderstats"
	
	-- player cash
	local Cash = Instance.new("NumberValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = leaderstats
	-- another folder to store button value
	local buttonfolder = Instance.new("Folder")
	buttonfolder.Name = "ButtonsFolder"
	buttonfolder.Parent = plr
	-- for gamepass
	local get2x = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId , gamespass2xID)
	local get3x = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId , gamespass3xID)
	local get5x = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId , gamespass5xID)
	
	-- creates gamepass values
		local pass2x = Instance.new("IntValue")
		pass2x.Name = "Cash2x"
		pass2x.Value = 1
		pass2x.Parent = buttonfolder
	if get2x then
		pass2x.Value = 2
	end
	
		local pass3x = Instance.new("IntValue")
		pass3x.Name = "Cash3x"
		pass3x.Value = 1
	pass3x.Parent = buttonfolder
	if get3x then
		pass3x.Value = 3
	end
	
		local pass5x = Instance.new("IntValue")
		pass5x.Name = "Cash5x"
		pass5x.Value = 1
	pass5x.Parent = buttonfolder
	if get5x then
		pass5x.Value = 5
	end
	
	
	-- for VIP gamepass
	local Gamepass =  game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId , gamepassID)
	-- for VIP
		local VIP = Instance.new("BoolValue")
		VIP.Name =  "VIP"
		VIP.Value = false
		VIP.Parent = plr
	if Gamepass then
		VIP.Value = true
	end
	
	-- when the button is purchased it will chaneg the value coresponding
	local button2 = Instance.new("BoolValue")
		button2.Name = "Button2"
		button2.Value = false
	button2.Parent = buttonfolder
	
	local button3 = Instance.new("BoolValue")
		button3.Name = "Button3"
		button3.Value = false
	button3.Parent = buttonfolder
	
	
	local button5= Instance.new("BoolValue")
		button5.Name ="Button5"
	    button5.Value=false
	button5.Parent=buttonfolder
	
	local button6 = Instance.new("BoolValue")
		button6.Name = "Button6"
		button6.Value = false
	button6.Parent = buttonfolder
	
	
	
	local button8= Instance.new("BoolValue")
		button8.Name ="Button8"
	    button8.Value=false
	button8.Parent=buttonfolder
		
	local button9= Instance.new("BoolValue")
		button9.Name ="Button9"
	    button9.Value=false
	button9.Parent=buttonfolder

	
	local button11 = Instance.new("BoolValue")
		button11.Name = "Button11"
		button11.Value = false
	button11.Parent = buttonfolder
	
	local button12= Instance.new("BoolValue")
		button12.Name ="Button12"
	    button12.Value=false
	button12.Parent=buttonfolder
	

	local button4VIP = Instance.new("BoolValue")
	button4VIP.Name = "Button4VIP"
	button4VIP.Value = false
	button4VIP.Parent = buttonfolder
	
	local button7VIP = Instance.new("BoolValue")
	button7VIP.Name = "Button7VIP"
	button7VIP.Value = false
	button7VIP.Parent = buttonfolder
	
	local button5VIP =  Instance.new("BoolValue")
	button5VIP.Name = "Button5VIP"
	button5VIP.Value = false
	button5VIP.Parent = buttonfolder
	
	local button6VIP =  Instance.new("BoolValue")
	button6VIP.Name = "Button6VIP"
	button6VIP.Value = false
	button6VIP.Parent = buttonfolder
	
	local button8VIP= Instance.new("BoolValue")
		button8VIP.Name ="Button8Vip"
	    button8VIP.Value=false
	button8VIP.Parent=buttonfolder
		
	local button9Vip= Instance.new("BoolValue")
		button9Vip.Name ="Button9Vip"
	    button9Vip.Value=false
	button9Vip.Parent=buttonfolder
		
	local button10vip =  Instance.new("BoolValue")
		button10vip.Name ="Button10Vip"
	    button10vip.Value=false
	button10vip.Parent= buttonfolder 
	
	local button11VIP =  Instance.new("BoolValue")
		button11VIP.Name = "Button11Vip"
		button11VIP.Value = false
	button11VIP.Parent = buttonfolder
	
	local button12VIP= Instance.new("BoolValue")
		button12VIP.Name ="Button12Vip"
	    button12VIP.Value=false
	button12VIP.Parent=buttonfolder
	
	-- Datasave 
	local success = nil
	local playerdata = nil 
	local attempt = 1
	repeat
		success, playerdata = pcall(function()
			 return database:GetAsync(plr.UserId)
		end)
		attempt += 1
		if not success then
			warn(playerdata)
			task.wait(3)
		end
	until success or attempt == 5
	
	if success then
		print("Connected to Database!")
		if not playerdata then
			print("Assigning Default Data")
			-- players Data
			playerdata = {
				-- add all the player instance value here in a data
				-- cash value
				["Cash"] = 0,
				-- VIP VALUE
				["VIP"] = false, 
				-- Boost value
				["Get2x"] = 1,
				["Get3x"] =  1,
				["Get5x"] = 1,
				-- button value 
				["Button2"] = false,
				["Button3"] = false,
				["Button4VIP"] = false,
				["Button5"] = false,
				["Button5VIP"] = false,
				["Button6"] = false,
				["Button6VIP"] = false,
				["Button7VIP"] = false,
				["Button8"] = false,
				["Button8VIP"] = false,
				["Button9"] = false,
				["Button9VIP"] = false,
				["Button10VIP"] = false,
				["Button11"] = false,
				["Button11VIP"] = false,
				["Button12"] = false,
				["Button12VIP"] = false
			}
		end
		sessiondata[plr.UserId] = playerdata
	else -- if can't get player data
		warn("Unable to get Data for", plr.Name)
		plr:Kick("Unable to load your Data! Please Try again Shortly!")
		
	end
	
	
	-- for button2 value 
	button2.Value = sessiondata[plr.UserId].Button2
	button2.Changed:Connect(function()
		sessiondata[plr.UserId].Button2 = button2.Value
	end)
	-- for button3 value
	button3.Value = sessiondata[plr.UserId].Button3
	button3.Changed:Connect(function()
		sessiondata[plr.UserId].Button3 = button3.Value
	end)
	-- for button 4vip value 
	button4VIP.Value = sessiondata[plr.UserId].Button4VIP
	button4VIP.Changed:Connect(function()
		sessiondata[plr.UserId].Button4VIP = button4VIP.Value
	end)
	-- for button 5 value 
	button5.Value = sessiondata[plr.UserId].Button5
	button5.Changed:Connect(function()
		sessiondata[plr.UserId].Button5 = button5.Value
	end)
	-- for button 5 vip
	button5VIP.Value = sessiondata[plr.UserId].Button5VIP
	button5VIP.Changed:Connect(function()
		sessiondata[plr.UserId].Button5VIP = button5VIP.Value
	end)
	-- for button 6
	button6.Value = sessiondata[plr.UserId].Button6
	button6.Changed:Connect(function()
		sessiondata[plr.UserId].Button6 = button6.Value
	end)
	-- for button 6 vip 
	button6VIP.Value = sessiondata[plr.UserId].Button6VIP
	button6VIP.Changed:Connect(function()
		sessiondata[plr.UserId].Button6VIP = button6VIP.Value
	end)
	-- for button 7 Vip 
	button7VIP.Value = sessiondata[plr.UserId].Button7VIP
	button7VIP.Changed:Connect(function()
		sessiondata[plr.UserId].Button7VIP = button7VIP.Value
	end)
	-- for button 8
	button8.Value = sessiondata[plr.UserId].Button8
	button8.Changed:Connect(function()
		sessiondata[plr.UserId].Button8 = button8.Value
	end)
	-- for button 8 vip
	button8VIP.Value = sessiondata[plr.UserId].Button8VIP
	button8VIP.Changed:Connect(function()
		sessiondata[plr.UserId].Button8VIP = button8VIP.Value
	end)
	-- for button 9
	button9.Value = sessiondata[plr.UserId].Button9
	button9.Changed:Connect(function()
		sessiondata[plr.UserId].Button9 = button9.Value
	end)
	-- for button 9 vip
	button9Vip.Value = sessiondata[plr.UserId].Button9VIP
	button9Vip.Changed:Connect(function()
		sessiondata[plr.UserId].Button9VIP = button9Vip.Value
	end)
	-- for button 10 vip
	button10vip.Value = sessiondata[plr.UserId].Button10VIP
	button10vip.Changed:Connect(function()
		sessiondata[plr.UserId].Button10VIP = button10vip.Value
	end)
	-- for button 11
	button11.Value = sessiondata[plr.UserId].Button11
	button11.Changed:Connect(function()
		sessiondata[plr.UserId].Button11 = button11.Value
	end)
	-- for button 11 vip
	button11VIP.Value = sessiondata[plr.UserId].Button11VIP
	button11VIP.Changed:Connect(function()
		sessiondata[plr.UserId].Button11VIP = button11VIP.Value
	end)
	-- for button 12
		button12.Value = sessiondata[plr.UserId].Button12
	button12.Changed:Connect(function()
		sessiondata[plr.UserId].Button12 = button12.Value
	end)
	-- for button 12 vip 
	button12VIP.Value = sessiondata[plr.UserId].Button12VIP
	button12VIP.Changed:Connect(function()
		sessiondata[plr.UserId].Button12VIP = button12VIP.Value
	end)
	buttonfolder.Parent = plr
	-- for 2x VALUE 
	pass2x.Value = sessiondata[plr.UserId].Get2x
	pass2x.Changed:Connect(function()
		sessiondata[plr.UserId].Get2x = pass2x.Value
	end)
	-- for 3x value 
	pass3x.Value = sessiondata[plr.UserId].Get3x
	pass3x.Changed:Connect(function()
		sessiondata[plr.UserId].Get3x = pass3x.Value
	end)
	-- for 5x value 
	pass5x.Value = sessiondata[plr.UserId].Get5x
	pass5x.Changed:Connect(function()
		sessiondata[plr.UserId].Get5x = pass5x.Value
	end)
	-- for VIP value 
	VIP.Value = sessiondata[plr.UserId].VIP
	VIP.Changed:Connect(function()
		sessiondata[plr.UserId].VIP = VIP.Value
	end)
	
	-- for cash value 
	Cash.Value = sessiondata[plr.UserId].Cash
	Cash.Changed:Connect(function()
		sessiondata[plr.UserId].Cash = Cash.Value
	end)
	leaderstats.Parent = plr
			
end


game.Players.PlayerAdded:Connect(Playeradded)

function playerleaving(player)
	-- stores the Data if the player is leaving
	if sessiondata[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1
		repeat
		success, errorMsg = pcall(function()
			database:SetAsync(player.UserId, sessiondata[player.UserId])
			end)
			attempt += 1
			if not success then
				warn(errorMsg)
				task.wait(3)
			end
		until success or attempt == 5
		
		if success then
			print("Data Saved!", player.Name)
		else
			warn("Unable to save Data!" , player.Name)
		end
	end
end
	
game.Players.PlayerRemoving:Connect(playerleaving)

can you please advice me to fix this code?

2 Likes

Sorry not really sure what your problem is can you elaborate? Like the leaderstat not showing up, not saving, etc.

This wil break the code entirely as playerdata in this case is actually an errorMessage, which if success is true, it will wipe the data and making it assign default values. I fixed that part for you:

-- Datasave 
	local success = nil
	local errorMessage = nil
	local playerdata = nil 
	local attempt = 1
	repeat
		success, errorMessage = pcall(function()
			playerdata = database:GetAsync(plr.UserId)
		end)
		attempt += 1
		if not success then
			warn(errorMessage)
			task.wait(3)
		end
	until success or attempt == 5
1 Like

My problem is that only cash values saves and rest (like button2, button3, pass2x, etc.) does not gets saved.

yeah but the video i watched to create it, they used “playerdata” for errormessage?

And also if the the data is not there then it would save the data, so if there is no data then it will create new data and store there?

in the script;

“if not playerdata then”
if the playerdata is not true then it will assign the data to it and if

“if success then”
then it will print “Connected to database”

i don’t think it might be wrong but i will look after the solution you have told!

From what I’m reading it seems like it should work, I’m not really used to commenting in the dev forum but do you know if your button values change? Like with a server script? Sorry if I’m missing anything I tried to look through it. Just double check. I’ll try to find anything else…

yes it change, when a purchase is made it will directly change the value to true in the specified button value!, Maybe it is a boolvalue that’s why it does not work?

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?