Data Store Print Returns Nil

The code Presented is supposed to save the equipped factor attribute and print whats been equipped while also saving the data but the print is returning nil?

Here is server Side

-- Script in ServerScriptService
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local EquippedFactorStore = DataStoreService:GetDataStore("EquippedFactorStore")

Players.PlayerAdded:Connect(function(player)
	local equippedFactor = "BasicFactor"
	local success, savedFactor = pcall(function()
    print(equippedFactor)
		return EquippedFactorStore:GetAsync(player.UserId)
	end)

	if success and savedFactor then
		equippedFactor = savedFactor
	else
		EquippedFactorStore:SetAsync(player.UserId, equippedFactor)
	end

	player:SetAttribute("EquippedFactor", equippedFactor)
end)

Players.PlayerRemoving:Connect(function(player)
	local equippedFactor = player:GetAttribute("EquippedFactor")
	EquippedFactorStore:SetAsync(player.UserId, equippedFactor)
end)


Here is the code that holds the print that is returning nil

function seteverythinginvisible()
	script.Parent.MainFrame.Popups.PlayPopup.Visible = false
	script.Parent.MainFrame.Popups.CustomizePopup.Visible = false
	script.Parent.MainFrame.Buttons.ButtonsGroup.Play.PlayButton.Active = true
	script.Parent.MainFrame.Buttons.ButtonsGroup.Inventory.CustomizeButton.Active = true
    
end

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local buttons = {
	Battlepassbutton = script.Parent.MainFrame.Buttons.ButtonsGroup["BattlePass "].BattlePassButton,
	PlayButton = script.Parent.MainFrame.Buttons.ButtonsGroup.Play.PlayButton,
	CustomizeButton = script.Parent.MainFrame.Buttons.ButtonsGroup.Inventory.CustomizeButton,
	InventoryFactors = script.Parent.MainFrame.Popups.CustomizePopup.CanvasGroup.Factors,
	CloseFactors = script.Parent.MainFrame.Popups.InventoryPopup.Close,
	Basicfactorbutton = script.Parent.MainFrame.Popups.InventoryPopup.ScrollingFrame.BasicFactor,
	ParasolFactorbutton = script.Parent.MainFrame.Popups.InventoryPopup.ScrollingFrame.ParasolKingFactor,
	InfinityFactorbutton = script.Parent.MainFrame.Popups.InventoryPopup.ScrollingFrame["Infinity Factor"],
    EquipButton = script.Parent.MainFrame.Popups.InventoryPopup.EquipPopup.Equipbutton,

}

local Popups = {
	CustomizePopup = script.Parent.MainFrame.Popups.CustomizePopup,
	PlayPopup = script.Parent.MainFrame.Popups.PlayPopup
}


buttons.CustomizeButton.MouseButton1Click:Connect(function()
	seteverythinginvisible()
	Popups.CustomizePopup.Visible = true
	buttons.CustomizeButton.Active = false

end)

buttons.PlayButton.MouseButton1Click:Connect(function()
	seteverythinginvisible()
	Popups.PlayPopup.Visible = true
    buttons.PlayButton.Active = false
end)

buttons.InventoryFactors.MouseButton1Click:Connect(function()
	script.Parent.MainFrame.Buttons.Visible = false
	script.Parent.MainFrame.Popups.CustomizePopup.Visible = false
	script.Parent.MainFrame.Popups.InventoryPopup.Visible = true
	
end)

buttons.CloseFactors.MouseButton1Click:Connect(function()
	script.Parent.MainFrame.Buttons.Visible = true
	script.Parent.MainFrame.Popups.CustomizePopup.Visible = true
	script.Parent.MainFrame.Popups.InventoryPopup.Visible = false

end)

buttons.Basicfactorbutton.MouseButton1Click:Connect(function()
	script.Parent.MainFrame.Popups.InventoryPopup.EquipPopup.Factorname.Text = "Basic Factor"
	local equippedFactor = player:GetAttribute("EquippedFactor")
	if equippedFactor == "BasicFactor" then
		print("this is basic factor")
		script.Parent.MainFrame.Popups.InventoryPopup.EquipPopup.Equipbutton.Text = "Equipped"
	else
		script.Parent.MainFrame.Popups.InventoryPopup.EquipPopup.Equipbutton.Text = "Equip"
		buttons.EquipButton.MouseButton1Click:Connect(function()
			local selectedFactor = "InfinityFactor"
			local equippedFactor = player:SetAttribute('EquippedFactor', "BasicFactor")
			script.Parent.MainFrame.Popups.InventoryPopup.EquipPopup.Equipbutton.Text = "Equipped"
		end)	
		
	end
end)

buttons.InfinityFactorbutton.MouseButton1Click:Connect(function()
	script.Parent.MainFrame.Popups.InventoryPopup.EquipPopup.Factorname.Text = "Infinity Factor"
	local equippedFactor = player:GetAttribute("EquippedFactor")
	if equippedFactor == "InfinityFactor" then
		print("this is Infinity factor")
		script.Parent.MainFrame.Popups.InventoryPopup.EquipPopup.Equipbutton.Text = "Equipped"
	else
		script.Parent.MainFrame.Popups.InventoryPopup.EquipPopup.Equipbutton.Text = "Equip"
		buttons.EquipButton.MouseButton1Click:Connect(function()
			local selectedFactor = "InfinityFactor"
			local equippedFactor = player:SetAttribute('EquippedFactor', "InfinityFactor")
			script.Parent.MainFrame.Popups.InventoryPopup.EquipPopup.Equipbutton.Text = "Equipped"
			print(equippedFactor)  -- This is returning nil?? yet for some reason the above code works like it is changed to equip when I press and when i try opening it again the other print returns as "this is infinity factor"
		end)	

	end
end)   

The code that needs to be reviewed is at the bottom

Instance:SetAttribute(name, value) does not return the value set. It doesn’t return anything, just nil.

That’s why the variable is printing as nil.

Do this instead:

local equipFactor = "InfinityFactor"
player:SetAttribute('EquipFactor', equipFactor) 

Hope this helped!

This does not change the value at all it prints BasicFactor instead of InfinityFactor

It might be because I messed up with the name of the variable not matching yours.

try:

local equippedFactor  = "InfinityFactor"
player:SetAttribute('EquippedFactor', equippedFactor) 

My bad, hope this one actually works.

Okay now things have got weird it changes to the infinity factor but when i try to change back to the basic factor it prints basic factor then snaps back to infinity factor

it prints basic factor
then prints infinity factor