How can I used a changed variable that was changed inside a function, outside of a function

I was working a game and realized I couldn’t use variables that I changed inside my function, outside of the function.

local realequipBtn = equipBTN -- variable


_G.newTemplate = function(petName)
	local newTemplate = template:Clone()
	local realtemplate = container:FindFirstChild(newTemplate.Name)
	newTemplate.Name = petName
	newTemplate.Visible = true
	newTemplate.Parent = container

	local petModel = Module3D:Attach3D(newTemplate.Display,replicatedStorage.Pets:FindFirstChild(petName, true):Clone())
	petModel:SetDepthMultiplier(replicatedStorage.Pets:FindFirstChild(petName, true).Size.Value)
	petModel.Camera.FieldOfView = 5
	petModel.Visible = true
	petModel.ZIndex = 5

	runService.RenderStepped:Connect(function()
		petModel:SetCFrame(CFrame.Angles(0, 10,0) * CFrame.Angles(math.rad(-10),0,0))
	end)

	local temp = newTemplate

	selectedTemplate = temp
	if selectedTemplate:FindFirstChild("Equipped").Value == true then
		selectedTemplate.Checkmark.Visible = true
	else
		selectedTemplate.Checkmark.Visible = false
	end
	selectedTemplate = temp 
	
	realequipBtn = newTemplate.Info.Equip ---changing variable 
	
end

--put changed variable here

How could I do this its been a problems for hours!

Using return

local function sayHi()
    local printThis = "Hi"
    return printThis
end
-- Print returned value of sayHi
print(sayHi())