Hello everyone, hope all is well.
I’m currently finding it very hard to implement new values to save in a DataStore
. Whenever I try to add something new, then nothing saves. How can I restructure this code, so it’s future-proof and efficient?
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local SettingsRemote = RemoteEvents.Settings
local FOVslider = DataStoreService:GetDataStore("Settings", "FOV")
local IGMslider = DataStoreService:GetDataStore("Settings", "IGM")
local PUNswitch = DataStoreService:GetDataStore("Settings", "PUN")
local UNIXswitch = DataStoreService:GetDataStore("Settings", "UnixTimestamp")
local FOVData, IGMData, PUNData, UNIXData
local function AutoSave(plr)
if plr then
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then
local FOV, IGM, PUN, UNNI = leaderstats:FindFirstChild("FOV_VALUE"), leaderstats:FindFirstChild("IGM_VALUE"), leaderstats:FindFirstChild("PUN_VALUE"), leaderstats:FindFirstChild("UNIX_VALUE")
if FOV and IGM and PUN and UNNI then
local success, errorMessage = pcall(function()
if FOV.Value ~= FOVData then
FOVslider:SetAsync(plr.UserId, FOV.Value)
end
if IGM.Value ~= IGMData then
IGMslider:SetAsync(plr.UserId, IGM.Value)
end
if PUN.Value ~= PUNData then
PUNswitch:SetAsync(plr.UserId, PUN.Value)
end
UNIXswitch:SetAsync(plr.UserId, UNNI.Value)
end)
if not success then
warn("Error saving data for "..plr.Name.." ("..errorMessage..")")
end
end
end
end
end
Players.PlayerAdded:Connect(function(Player)
-- FIELD OF VIEW
local FOV = Instance.new("IntValue")
local FOVdefaultValue = 50
FOV.Name = "FOV_VALUE"
FOV.Parent = Player:WaitForChild("leaderstats")
FOV.Value = FOVdefaultValue -- Default Value
SettingsRemote:FireClient(Player, "FOV", FOV.Value) -- Default Value
local success, err = pcall(function()
FOVData = FOVslider:GetAsync(Player.UserId)
end)
SettingsRemote:FireClient(Player, "FOV", FOVData)
if success and FOVData then
FOV.Value = FOVData
elseif FOVData == nil then
FOV.Value = FOVdefaultValue
print("Oh! You must be new! We're setting you up...")
print("Welcome to Miles by the way. :)")
print(" ")
end
-- IN GAME MUSIC
local IGM = Instance.new("NumberValue")
local IGMdefaultValue = 0.8
IGM.Name = "IGM_VALUE"
IGM.Parent = Player:WaitForChild("leaderstats")
IGM.Value = IGMdefaultValue -- Default Value
SettingsRemote:FireClient(Player, "IGM", IGM.Value) -- Default Value
local success, err = pcall(function()
IGMData = IGMslider:GetAsync(Player.UserId)
end)
SettingsRemote:FireClient(Player, "IGM", IGMData)
if success and IGMData then
IGM.Value = IGMData
elseif IGMData == nil then
IGM.Value = IGMdefaultValue
end
-- POWER-UP NOTIFICATION
local PUN = Instance.new("BoolValue")
local PUNdefaultValue = true
PUN.Name = "PUN_VALUE"
PUN.Parent = Player:WaitForChild("leaderstats")
PUN.Value = PUNdefaultValue -- Default Value
SettingsRemote:FireClient(Player, "PUN", PUN.Value) -- Default Value
local success, err = pcall(function()
PUNData = PUNswitch:GetAsync(Player.UserId)
end)
SettingsRemote:FireClient(Player, "PUN", PUNData)
if success and PUNData then
PUN.Value = PUNData
elseif PUNData == nil then
PUN.Value = PUNdefaultValue
end
-- UNIX TIMESTAMP SETTING (first join)
local UNI = Instance.new("NumberValue")
local UNIdefaultValue = 0
UNI.Name = "UNIX_VALUE"
UNI.Parent = Player:WaitForChild("leaderstats")
UNI.Value = UNIdefaultValue -- Default Value
SettingsRemote:FireClient(Player, "UNI", UNI.Value) -- Default Value
local success, err = pcall(function()
UNIXData = UNIXswitch:GetAsync(Player.UserId)
end)
SettingsRemote:FireClient(Player, "UNI", UNIXData)
if success and UNIXData then
if UNIXData == 0 then -- New player, set OS Time
UNI.Value = os.time()
else
UNI.Value = UNIXData
end
elseif UNIXData == nil then -- ^ ^ Assume new join ^ ^
UNI.Value = UNIXData or 0
end
while task.wait(300) do
print("Saved Data!")
AutoSave(Player)
end
end)
SettingsRemote.OnServerEvent:Connect(function(Player, Type, Value)
local leaderstats = Player:FindFirstChild("leaderstats")
if Type == "FOV" then
if leaderstats then
local FOV = leaderstats:FindFirstChild("FOV_VALUE")
if FOV then
FOV.Value = Value
end
end
elseif Type == "IGM" then
if leaderstats then
local IGM = leaderstats:FindFirstChild("IGM_VALUE")
if IGM then
IGM.Value = Value
end
end
elseif Type == "PUN" then
if leaderstats then
local PUN = leaderstats:FindFirstChild("PUN_VALUE")
if PUN then
PUN.Value = Value
end
end
elseif Type == "UNI" then
if leaderstats then
local UNIXX = leaderstats:FindFirstChild("UNIX_VALUE")
if UNIXX then
UNIXX.Value = Value
end
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
AutoSave(Player)
end)
Thanks, Aki
(yes I’m very bad at DataStores
, if anything they’re one of my worst nightmares)