DataStore Not Working!

Hello , I am recently new to scripting and Stumbled upon a problem, so in the given below code. I have some values and in those only “Cash” Value work out and rest of all does not even store, So can you resolve/Give advice for this issue

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

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
	
	
	-- 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 errorMessage = 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
	
	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
	remotevent:FireClient(plr, true)	
	if Gamepass then
		VIP.Value = true
	end
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)	

Thank you,

6 Likes

I would recommending adding game:BindToClose()

3 Likes

Hey, i will try but what it can do?

3 Likes

If roblox crashes, shutdowns, and basically it further enforces your datastore in case if it didn’t save or if they lose their data

3 Likes

(further note is that the player doesn’t technically leave so BindToClose is good, and probably a must)

2 Likes

it is technically like a wait function right? but my problem is not that, my problem is that only cash value saves and rest of them does not saves!

1 Like

I assume that could be the issue since there is no BindToClose imagine like a repeat wait until everything is saved (data) or the function is done.

1 Like

yeah should i put it in the end of the script or the starting of the script?

1 Like

The only exception to this is when servers crash when they reach above the server memory limit of 6.25 GB, the entire server is terminated which means BindToClose doesn’t work. @colourfullsteve08

Yeah then what should i do to improve it or other functions?

End

[[EXTRRAAAAAACHARRACTERSOVERHERE]]

2 Likes

You can’t do much, just make sure you have BindToClose with a save function in it and an autosave so you can ensure when servers crash that data won’t be lost (at least majorly in most cases)

2 Likes

but still the game i am making will maybe not even extented above 4 gb memory i think so

1 Like

Still have a BindToClose function because when you shut down servers or if ROBLOX servers go down, you have that function to save everyone’s data

1 Like

Furthermore, use a for i,v loop.

Like this

Game:BindToClose(function(plr)
   For i, player in pairs(game.Players:GetPlayers()) do
            — Data goes here
      end

end)

You should also add a pcall function

1 Like

I wrote this in mobile so sorry for any errors

1 Like

Add a BindToClose function at the end of code:

game:BindToClose(function()
	for Index, Player in Players:GetPlayers() do -- Add Players service: local Players = game:GetService('Players')
		pcall(function() playerleaving(Player) end)
	end
end)
3 Likes

Why not for Index, Player in pairs(Player:GetPlayers())

1 Like

Recent Roblox update where you don’t have to include ipairs or pairs, does the same thing.

2 Likes

Hmm. Never heard of the update. I’ll stick to pairs() cause I’m used to it

1 Like