Variable not changing?

Hey guys. So I am trying to make a command that toggles noclip for your self. But instead of it toggling like I want it to it just turns on and stays on? What do I do?


local function pa_selfNoclip()
	local ply = LocalPlayer()
	local isActive= false
	
	if(isActive== false) then
		isActive= true
		net.Start("selfclipOn")
		net.WriteFloat(ply:UserID())
		net.SendToServer()
	else
		isActive= false
		net.Start("selfclipOff")
		net.WriteFloat(ply:UserID())
		net.SendToServer()
	end
end
concommand.Add("pa_selfNoclip", pa_selfNoclip)


local isActive= false

Every time you run the console command, you are re-declaring isActive to be false no matter what (making them go into noclip regardless of how many times you run it).

Why not make the console command serverside and store the variable (isActive) on the player there? You wouldn’t need net messages to do it.

Something like this (untested, never manually set noclip either):



-- some serverside Lua file
concommand.Add( "pa_selfNoclip", function( ply, args, cmd )
    ply.NoClipActive = !ply.NoClipActive

    ply:SetMoveType( ( ply.NoClipActive and MOVETYPE_NOCLIP ) or MOVETYPE_WALK )
end )