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?