Issues with drawing a scope when zoomed in

So I have been trying to draw a scope when zooming in on my SWEP, but even though I change a variable in another function it seems to refuse to change in SWEP:DrawHUD. I set the scoped state in SWEP:SecondaryAttack() and assume that it should be also updated on SWEP:DrawHUD()

-- This will activate and deactivate the scope zoom
function SWEP:SecondaryAttack()
    local owner = self:GetOwner()
    if not self.Scoped then
        owner:SetFOV(self.ZoomFOV, self.ZoomTransition)
        owner:CrosshairDisable()
        self.Scoped = true
    else
        owner:SetFOV(0, self.ZoomTransition)
        owner:CrosshairEnable()
        self.Scoped = false
    end
end

if CLIENT then
-- Draws scope on HUD when zoomed in (currently doesn't work)
    function SWEP:DrawHUD()
        if self.Scoped then

            local width, height = 687, 687

            local x, y = (ScrW() / 2) - (width / 2) , (ScrH() / 2) - (height / 2)      -- Center of the screen
            
            surface.SetMaterial(Material("materials/weapons/scope.png"))
            surface.SetDrawColor(255, 255, 255, 255)
            surface.DrawTexturedRect(x, y, width, height)
        end
    end

    function SWEP:AdjustMouseSensitivity()
        if self.Scoped then return 0.25 end
    end
end

If anyone has any insight as to why this is wrong, that would be great!

EDIT: Forgot to add screenshot lol

I figured it out! You just need to use GetNWBool and SetNWBool as such:

-- This will activate and deactivate the scope zoom
function SWEP:SecondaryAttack()
    local owner = self:GetOwner()
    if not self:GetNWBool("Scoped") then
        owner:SetFOV(self.ZoomFOV, self.ZoomTransition)
        owner:CrosshairDisable()
        self:SetNWBool("Scoped", true)
    else
        owner:SetFOV(0, self.ZoomTransition)
        owner:CrosshairEnable()
        self:SetNWBool("Scoped", false)
    end
end


if CLIENT then
-- Draws scope on HUD when zoomed in (currently doesn't work)
    function SWEP:DrawHUD()
        if self:GetNWBool("Scoped") then

            local width, height = 687, 687

            local x, y = (ScrW() / 2) - (width / 2) , (ScrH() / 2) - (height / 2)      -- Center of the screen
            
            surface.SetMaterial(Material("materials/weapons/scope.png"))
            surface.SetDrawColor(255, 255, 255, 255)
            surface.DrawTexturedRect(x, y, width, height)
        end
    end

    function SWEP:AdjustMouseSensitivity()
        if self:GetNWBool("Scoped") then return 0.25 end
    end
end

Posting this for anyone else who might need to know from a Google search.

1 Like